Capture User Input In Xamarin Android App And Validate It

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.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent">  
  6.     <EditText  
  7.         android:layout_marginLeft="20dp"  
  8.         android:inputType="textEmailAddress"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:id="@+id/txtUserValue"  
  12.         android:hint="Enter Email Id"  
  13.         android:layout_marginRight="0.0dp" />  
  14.     <EditText  
  15.         android:layout_marginLeft="20dp"  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:id="@+id/txtWebURL"  
  19.         android:layout_marginRight="0.0dp"  
  20.         android:hint="Enter Web URL" />  
  21.     <TextView  
  22.         android:layout_marginLeft="20dp"  
  23.         android:text=""  
  24.         android:layout_width="match_parent"  
  25.         android:layout_height="wrap_content"  
  26.         android:id="@+id/txtResult" />  
  27.     <TextView  
  28.         android:layout_marginLeft="20dp"  
  29.         android:text=""  
  30.         android:layout_width="match_parent"  
  31.         android:layout_height="wrap_content"  
  32.         android:id="@+id/txtWebResult" />  
  33.     <Button  
  34.         android:text="Button"  
  35.         android:layout_width="273.5dp"  
  36.         android:layout_height="wrap_content"  
  37.         android:layout_marginLeft="40dp"  
  38.         android:id="@+id/btnSubmit" />  
  39. </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.
  1. namespace Validate_User_Input_Xamarin  
  2. {  
  3.     [Activity(Label = "Capture User Input", MainLauncher = true, Icon = "@drawable/icon")]  
  4.     public class MainActivity : Activity  
  5.     {  
  6.         protected override void OnCreate(Bundle bundle)  
  7.         {  
  8.             base.OnCreate(bundle);  
  9.             SetContentView(Resource.Layout.Main);  
  10.             Button submit = FindViewById<Button>(Resource.Id.btnSubmit);  
  11.             submit.Click += delegate  
  12.            {  
  13.                // copy the user entered text into variable  
  14.                var userEmail = FindViewById<EditText>(Resource.Id.txtUserValue);  
  15.                string userValue = userEmail.Text.ToString();  
  16.                var showResult = FindViewById<TextView>(Resource.Id.txtResult);  
  17.                var emailResult = isValidEmail(userValue);  
  18.                if(userValue == "")  
  19.                {  
  20.                    showResult.Text = "Please enter email id";  
  21.                }  
  22.                else  
  23.                {  
  24.                    if (emailResult == true)  
  25.                    {  
  26.                        showResult.Text = "Great...Email id is valid";  
  27.                    }  
  28.                    else  
  29.                    {  
  30.                        showResult.Text = "Wrong email id. Try again";  
  31.                    }  
  32.                }  
  33.                var userWebURL = FindViewById<EditText>(Resource.Id.txtWebURL);  
  34.                string userWeb = userWebURL.Text.ToString();  
  35.                var showWebResult = FindViewById<TextView>(Resource.Id.txtWebResult);  
  36.                var webResult = isValidURL(userWeb);  
  37.                if (userWeb == "")  
  38.                {  
  39.                    showWebResult.Text = "Please enter URL";  
  40.                }  
  41.                else  
  42.                {  
  43.                    if (webResult == true)  
  44.                    {  
  45.                        showWebResult.Text = "Great...URL is valid";  
  46.                    }  
  47.                    else  
  48.                    {  
  49.                        showWebResult.Text = "Wrong URL. Try again";  
  50.                    }  
  51.                }  
  52.            };  
  53.         }  
  54.         // check user entered email id is valid or not  
  55.         public bool isValidEmail(string email)  
  56.         {  
  57.             return Android.Util.Patterns.EmailAddress.Matcher(email).Matches();  
  58.         }  
  59.         // check user entered URL is valid or not  
  60.         public bool isValidURL(string url)  
  61.         {  
  62.             return Android.Util.Patterns.WebUrl.Matcher(url).Matches();  
  63.         }  
  64.     }  
  65. }  
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.


Similar Articles