How To Validate A Phone Number In Xamarin Android App Using Visual Studio 2015

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.

For my other article on how to validate an email address, please go through the below link.
Prerequisites
  • Visual Studio 2015 Update 3.

The steps, given below, are required to be followed in order to validate a PhoneNumber in the Xamarin Android app, using Visual Studio 2015.

Step 1

Click File-->New--> Project. The project needs to be clicked after opening all the types of projects in Visual Studio. Or, press "Ctrl+Shift+N".

Visual Studio

Step 2

After opening the New Project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android). Now, give your Android app a name (Ex:sample) and give the path of your project. Afterwards, click OK.

Visual Studio

Step 3

Next, go to the Solution Explorer. Select Resource-->Layout-->double click to open Main.axml page.

Visual Studio

Step 4

This will open the main page designer. In this page, you can either select the Designer mode or Source mode to design the main page of your app.

Visual Studio

Next, delete the default "hello world button". For that, go to the source panel and remove the button coding. Then, delete the C# button action code. For that, go to the MainActivity.cs page and delete the button code. 

Step 5

Now, go to the Toolbox window. There, scroll down to see all the tools and controls. You need to drag and drop the Number.

Visual Studio

Step 6

Drag and drop the TextView.

Visual Studio

Step 7

Drag and drop the Button.

Visual Studio

Step 8

Now, go to the Properties window. You need to edit the Number Id Value (EX: android:id="@+id/txtnumber").

Visual Studio

Step 9

Now, edit the TextView Id Value(EX: android:id="@+id/txtdisplay" ).

Visual Studio

Step 10

And also, edit the Button Id value and Text value.(Ex: android:id="@+id/btnvalidate" android:text="Validate").

Visual Studio

Step 11

In this step, go to the Main.axml page Source Panel. Note the EditText Id value, TextView id value and Button Id Value.

Main.axml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:minWidth="25px" android:minHeight="25px">  
  2.   
  3.     <EditText android:inputType="number" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/txtnumber" android:hint="Enter 6_Digit Phone Number" />  
  4.   
  5.     <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/txtdisplay" />  
  6.   
  7.     <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnvalidate" android:text="Validate" />  
  8. </LinearLayout>  
Visual Studio

Step 12

In this step, create a method called isValidPhone in MainActivity.cs page.

MainActivity.cs

  1. public bool isValidPhone(string phone) {  
  2.     return Android.Util.Patterns.Phone.Matcher(phone).Matches();  
  3. }  

Visual Studio

Step 13

In this step, go to the MainActivity.cs page in Solution Explorer. Write the following code in OnCreate() Method.

MainActivity.cs

  1. protected override void OnCreate(Bundle bundle) {  
  2.     base.OnCreate(bundle);  
  3.     SetContentView(Resource.Layout.Main);  
  4.     Button submit = FindViewById < Button > (Resource.Id.btnvalidate);  
  5.     submit.Click += delegate {  
  6.         EditText phone = FindViewById < EditText > (Resource.Id.txtnumber);  
  7.         string inputphone = phone.Text.ToString();  
  8.         TextView textdisplay = FindViewById < TextView > (Resource.Id.txtdisplay);  
  9.         var phonevalidate = isValidPhone(inputphone);  
  10.         if (inputphone == "") {  
  11.             textdisplay.Text = "Enter the 6-Digit PhoneNumber.!";  
  12.         } else {  
  13.             if (phonevalidate == true) {  
  14.                 textdisplay.Text = "PhoneNumber is Valid";  
  15.             } else {  
  16.                 textdisplay.Text = "PhoneNumber is Not Valid";  
  17.             }  
  18.         }  
  19.     };  
  20. }  
Visual Studio

Step 14

If you have Android Virtual device, run the app on it. Otherwise, connect your Android phone and run the app in that.

Simply connect your phone and go to Visual Studio. The connected phone will show up in the Run menu (Ex:LENOVO A6020a40 (Android 5.1-API 22)). Click the Run option.

Visual Studio

Output

After a few seconds, the app will start running on your phone.

Visual Studio

Now, when you give the less than six digit phone number and click on the validate button, it shows the error - "PhoneNumber is Not valid"

Visual Studio

When you give a six digit PhoneNumber, you will see the text value - "PhoneNumber is Valid".

Visual Studio

Summary

So, this was the process of validating a Phone number in Xamarin Android app, using Visual Studio 2015.

Up Next
    Ebook Download
    View all
    Learn
    View all