Cross Platform - Developing A Simple Android App Using Visual Studio With Xamarin - Part Two

Introduction

This writing is a continuation of my previous one, and here, we will be working on translating alphanumeric to numeric and getting a call function, using Android app.

Help

Look for my previous writings before you step into this one. Also, check for the requirements in the same.

Translating alphanumeric to numeric

In my previous work, I stopped with creating text box, buttons, button for call etc., and here I will be working on the translation of alphanumeric to numeric.

Right click on the solution name under Solution Explorer. Add - New Item.

Under New Item dialog box, select Visual C# - Code and name the code as PhoneTranslator.cs. We will be adding some code here, translating phone numbers from alphanumeric to numeric.


Click Add once after naming the code file.


You will be getting an empty C# class once. After clicking Add, remove all the template code which is available and replace it with the code given below. 

  1. using System.Text;  
  2. using System;  
  3. namespace Core  
  4. {  
  5.     public static class PhonewordTranslator  
  6.     {  
  7.         public static string ToNumber(string raw)  
  8.         {  
  9.             if (string.IsNullOrWhiteSpace(raw))  
  10.                 return "";  
  11.             else  
  12.                 raw = raw.ToUpperInvariant();  
  13.   
  14.             var newNumber = new StringBuilder();  
  15.             foreach (var c in raw)  
  16.             {  
  17.                 if (" -0123456789".Contains(c))  
  18.                     newNumber.Append(c);  
  19.                 else {  
  20.                     var result = TranslateToNumber(c);  
  21.                     if (result != null)  
  22.                         newNumber.Append(result);  
  23.                 }  
  24.                 // otherwise we've skipped a non-numeric char  
  25.             }  
  26.             return newNumber.ToString();  
  27.         }  
  28.         static bool Contains (this string keyString, char c)  
  29.         {  
  30.             return keyString.IndexOf(c) >= 0;  
  31.         }  
  32.         static int? TranslateToNumber(char c)  
  33.         {  
  34.             if ("ABC".Contains(c))  
  35.                 return 2;  
  36.             else if ("DEF".Contains(c))  
  37.                 return 3;  
  38.             else if ("GHI".Contains(c))  
  39.                 return 4;  
  40.             else if ("JKL".Contains(c))  
  41.                 return 5;  
  42.             else if ("MNO".Contains(c))  
  43.                 return 6;  
  44.             else if ("PQRS".Contains(c))  
  45.                 return 7;  
  46.             else if ("TUV".Contains(c))  
  47.                 return 8;  
  48.             else if ("WXYZ".Contains(c))  
  49.                 return 9;  
  50.             return null;  
  51.         }  
  52.     }  
  53. }  

Click File - Save or Ctrl + S to save the file, once you complete adding the file.

Now, open MainActivity.cs under Solution Explorer, as shown below.

Under MainActivity Class, find OnCreate method and add the button code inside OnCreate, below the base.OnCreate(bundle) and SetContentView (Resource.Layout.Main) calls.


Now, add the code given below inside OnCreate method after the call to SetContentView. 

  1. EditText phoneNumberText = FindViewById<EditText>(Resource.Id.PhoneNumberText);    
  2. Button translateButton = FindViewById<Button>(Resource.Id.TranslateButton);  
  3. Button callButton = FindViewById<Button>(Resource.Id.CallButton);  


Add the code given below for translate button under OnCreate method, place it after the last step.
  1. // Disable the "Call" button  
  2. callButton.Enabled = false;  
  3. // Add code to translate number  
  4. string translatedNumber = string.Empty;  
  5. translateButton.Click += (object sender, EventArgs e) => {  
  6.     // Translate user's alphanumeric phone number to numeric  
  7.     translatedNumber = Core.PhonewordTranslator.ToNumber(phoneNumberText.Text);  
  8.     if (String.IsNullOrWhiteSpace(translatedNumber)) {  
  9.         callButton.Text = "Call";  
  10.         callButton.Enabled = false;  
  11.     } else {  
  12.         callButton.Text = "Call " + translatedNumber;  
  13.         callButton.Enabled = true;  
  14.     }  
  15. };  

As an overview, your code for MainActivity.cs is supposed to resemble the same, as given below. 

  1. using System;  
  2. using Android.App;  
  3. using Android.Content;  
  4. using Android.Runtime;  
  5. using Android.Views;  
  6. using Android.Widget;  
  7. using Android.OS;  
  8.   
  9. namespace demoandroid  
  10. {  
  11.     [Activity(Label = "demoandroid", MainLauncher = true, Icon = "@drawable/icon")]  
  12.     public class MainActivity : Activity  
  13.     {  
  14.         protected override void OnCreate(Bundle bundle)  
  15.         {  
  16.             base.OnCreate(bundle);  
  17.   
  18.   
  19.             // Set our view from the "main" layout resource  
  20.             SetContentView(Resource.Layout.Main);  
  21.   
  22.             EditText phoneNumberText = FindViewById<EditText>(Resource.Id.PhoneNumberText);  
  23.             Button translateButton = FindViewById<Button>(Resource.Id.TranslateButton);  
  24.             Button callButton = FindViewById<Button>(Resource.Id.CallButton);  
  25.   
  26.             // Disable the "Call" button  
  27.             callButton.Enabled = false;  
  28.   
  29.             // Add code to translate number  
  30.             string translatedNumber = string.Empty;  
  31.   
  32.             translateButton.Click += (object sender, EventArgs e) =>  
  33.             {  
  34.                 // Translate user's alphanumeric phone number to numeric  
  35.                 translatedNumber = Core.PhonewordTranslator.ToNumber(phoneNumberText.Text);  
  36.                 if (String.IsNullOrWhiteSpace(translatedNumber))  
  37.                 {  
  38.                     callButton.Text = "Call";  
  39.                     callButton.Enabled = false;  
  40.                 }  
  41.                 else  
  42.                 {  
  43.                     callButton.Text = "Call " + translatedNumber;  
  44.                     callButton.Enabled = true;  
  45.                 }  
  46.             };  
  47.             callButton.Click += (object sender, EventArgs e) =>  
  48.             {  
  49.                 // On "Call" button click, try to dial phone number.  
  50.                 var callDialog = new AlertDialog.Builder(this);  
  51.                 callDialog.SetMessage("Call " + translatedNumber + "?");  
  52.                 callDialog.SetNeutralButton("Call", delegate {  
  53.                     // Create intent to dial phone  
  54.                     var callIntent = new Intent(Intent.ActionCall);  
  55.                     callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber));  
  56.                     StartActivity(callIntent);  
  57.                 });  
  58.                 callDialog.SetNegativeButton("Cancel", delegate { });  
  59.   
  60.                 // Show the alert dialog to the user and wait for response.  
  61.                 callDialog.Show();  
  62.             };  
  63.         }  
  64.     }  
  65. }   

Now, move for properties under Solution Explorer and add permission to set a phone call.


Click Android Manifest under Properties, go for Required Permissions – enable CALL_PHONE permission.


Now, save the solution file with Ctrl + S or File - Save all and build the solution.

Use Build - Rebuild Solution or Ctrl + Shift + B to build the program. You can find the program, which should be built with zero error and the app will be deployed on your device or an Emulator, which you have choosen.

Here, in the image given below, you can find my phone with demo Android app.


You will be getting the app, which is shown below.


Enter some text and click Translate. This will translate the text to numbers. Here, in my case, I have given the name “Najuma Mahamuth” and it has been shifted into some numerics.


Clicking on Call will allow you to call the number, which you have given.


Keynotes in short

  1. Adding buttons in an Android app.
  2. Adding a new class to translate alphanumeric to numerics.
  3. Add the code for MainActivity.cs
  4. Set call permission for an Android app under Android Manifest.
  5. Build and run the program on your device.

Up Next
    Ebook Download
    View all
    Learn
    View all