Introduction
Xamarin is a platform to develop cross-platform and multi-platform apps (like Windows phone, Android, iOS). In Xamarin, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.
Step 1
Create a new blank Android App using Visual Studio.
Step 2
In your project solution, there is a Main.axml file. Now, open this file in designer view. Drag and drop one email field to take an email input. Plain text is required to take Web URL from the user also. Drag two TextViews to show the result and take one button.
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <EditText
- android:layout_marginLeft="20dp"
- android:inputType="textEmailAddress"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/txtUserValue"
- android:hint="Enter Email Id"
- android:layout_marginRight="0.0dp" />
- <EditText
- android:layout_marginLeft="20dp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/txtWebURL"
- android:layout_marginRight="0.0dp"
- android:hint="Enter Web URL" />
- <TextView
- android:layout_marginLeft="20dp"
- android:text=""
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/txtResult" />
- <TextView
- android:layout_marginLeft="20dp"
- android:text=""
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/txtWebResult" />
- <Button
- android:text="Button"
- android:layout_width="273.5dp"
- android:layout_height="wrap_content"
- android:layout_marginLeft="40dp"
- android:id="@+id/btnSubmit" />
- </LinearLayout>
Step 3
Now, in MainActivity.cs file, get references to the EditText and the TextView. I am going to write the code to check if input is valid or not. I am going to explain how to validate an Email address and Web URL.
We can also validate other things like Domain Name, Top Level Domain, IP Address, Phone, Numbers etc.
Write the code, mentioned below to MainActivity.cs.
- namespace Validate_User_Input_Xamarin
- {
- [Activity(Label = "Capture User Input", MainLauncher = true, Icon = "@drawable/icon")]
- public class MainActivity : Activity
- {
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- SetContentView(Resource.Layout.Main);
- Button submit = FindViewById<Button>(Resource.Id.btnSubmit);
- submit.Click += delegate
- {
-
- var userEmail = FindViewById<EditText>(Resource.Id.txtUserValue);
- string userValue = userEmail.Text.ToString();
- var showResult = FindViewById<TextView>(Resource.Id.txtResult);
- var emailResult = isValidEmail(userValue);
- if(userValue == "")
- {
- showResult.Text = "Please enter email id";
- }
- else
- {
- if (emailResult == true)
- {
- showResult.Text = "Great...Email id is valid";
- }
- else
- {
- showResult.Text = "Wrong email id. Try again";
- }
- }
- var userWebURL = FindViewById<EditText>(Resource.Id.txtWebURL);
- string userWeb = userWebURL.Text.ToString();
- var showWebResult = FindViewById<TextView>(Resource.Id.txtWebResult);
- var webResult = isValidURL(userWeb);
- if (userWeb == "")
- {
- showWebResult.Text = "Please enter URL";
- }
- else
- {
- if (webResult == true)
- {
- showWebResult.Text = "Great...URL is valid";
- }
- else
- {
- showWebResult.Text = "Wrong URL. Try again";
- }
- }
- };
- }
-
- public bool isValidEmail(string email)
- {
- return Android.Util.Patterns.EmailAddress.Matcher(email).Matches();
- }
-
- public bool isValidURL(string url)
- {
- return Android.Util.Patterns.WebUrl.Matcher(url).Matches();
- }
- }
- }
Step 4
Now, run the Application on emulator followed by the screenshot, mentioned below.
If we click on the button without entering any values, then we will see the screenshot, mentioned below.
If we enter wrong values, then we will see the screenshot, mentioned below.
If we enter correct values, then we will see the screenshot, mentioned below..
Summary
This article will help the fresher candidates on how to capture the user input in Xamarin Android app and validate it.