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.
- 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".
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.
Step 3
Next, go to the Solution Explorer. Select Resource-->Layout-->double click to open Main.axml page.
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.
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.
Step 6
Drag and drop the TextView.
Step 7
Drag and drop the Button.
Step 8
Now, go to the Properties window. You need to edit the Number Id Value (EX: android:id="@+id/txtnumber").
Step 9
Now, edit the TextView Id Value(EX: android:id="@+id/txtdisplay" ).
Step 10 And also, edit the Button Id value and Text value.(Ex: android:id="@+id/btnvalidate" android:text="Validate").
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 - <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">
-
- <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" />
-
- <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/txtdisplay" />
-
- <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnvalidate" android:text="Validate" />
- </LinearLayout>
Step 12 In this step, create a method called isValidPhone in MainActivity.cs page. MainActivity.cs - public bool isValidPhone(string phone) {
- return Android.Util.Patterns.Phone.Matcher(phone).Matches();
- }
Step 13
In this step, go to the MainActivity.cs page in Solution Explorer. Write the following code in OnCreate() Method.
MainActivity.cs
- protected override void OnCreate(Bundle bundle) {
- base.OnCreate(bundle);
- SetContentView(Resource.Layout.Main);
- Button submit = FindViewById < Button > (Resource.Id.btnvalidate);
- submit.Click += delegate {
- EditText phone = FindViewById < EditText > (Resource.Id.txtnumber);
- string inputphone = phone.Text.ToString();
- TextView textdisplay = FindViewById < TextView > (Resource.Id.txtdisplay);
- var phonevalidate = isValidPhone(inputphone);
- if (inputphone == "") {
- textdisplay.Text = "Enter the 6-Digit PhoneNumber.!";
- } else {
- if (phonevalidate == true) {
- textdisplay.Text = "PhoneNumber is Valid";
- } else {
- textdisplay.Text = "PhoneNumber is Not Valid";
- }
- }
- };
- }
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. Output After a few seconds, the app will start running on your phone. 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" When you give a six digit PhoneNumber, you will see the text value - "PhoneNumber is Valid". Summary
So, this was the process of validating a Phone number in Xamarin Android app, using Visual Studio 2015.