Alphabets to Numbers Translator For Calling In Xamarin.Android

Introduction

In this article, we will learn how to build Call Translator App for Xamarin Android. This is a simple app for learning some basics for beginners and you can also create it in Android Studio but I like to make it in Visual Studio, therefore, I developed it in Xamarin. I will use C# language for logical code and AXML for layout. I used the name Call Translator, which means if you are not using QWERTY keyboard on your mobile phone and you type A, or B, or C, it will be considered as number 2 and if you type D, or E, or F, it will be considered as number 3 and so on. This app will convert your alphabets to numbers. Simply click on the "Translate" button for converting and after converting, you can call on the converted numbers.

Step 1

Create/ open your Android solution in Visual Studio or Xamarin Studio. Select Android and from the list, choose Android Blank App.

Step 2

First of all, we need TextView for showing a message and TextEdit for writing the text. There are two buttons for Translating and for Calling, so go to the Solution Explorer and open the Resources folder.

Inside the Resources folder, open Layout folder and double click on AXML file. Then, inside the AXML file, create the TextView, EditText, and two buttons followed by setting their properties - Names and Ids, as shown below.

(File Name: Main.axml) 

  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.     <TextView  
  7.         android:text="Enter a Phone Number"  
  8.         android:textAppearance="?android:attr/textAppearanceLarge"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:id="@+id/textView" />  
  12.     <EditText  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:id="@+id/phoneNumberText"  
  16.         android:text="+92300-Ahsan" />  
  17.     <Button  
  18.         android:text="Translate"  
  19.         android:layout_width="match_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:id="@+id/btnTranslate" />  
  22.     <Button  
  23.         android:text="Call"  
  24.         android:layout_width="match_parent"  
  25.         android:layout_height="wrap_content"  
  26.         android:id="@+id/btnCall" />  
  27. </LinearLayout>  

The designed from will look like below.

Xamarin

Step 3

Now, create a new class in this Solution as CallTranslator for Call Translator and in this class, define a method of translating the calls.

(FileName: CallTranslator.cs) 

  1. public static class CallTranslator  
  2.     {  
  3.         public static string ToNumber(string raw)  
  4.         {  
  5.             if (string.IsNullOrWhiteSpace(raw))  
  6.                 return "";  
  7.             else  
  8.             {  
  9.                 raw = raw.ToUpperInvariant();  
  10.             }  
  11.               
  12.             var newNumber = new StringBuilder();  
  13.   
  14.             foreach (var c in raw)  
  15.             {  
  16.                 if (" -0123456789".Contains(c))  
  17.                     newNumber.Append(c);  
  18.                 else  
  19.                 {  
  20.                     var result = TranslateNumber(c);  
  21.                     if (result != null)  
  22.                         newNumber.Append(result);  
  23.                 }  
  24.             }  
  25.             return newNumber.ToString();  
  26.         }  
  27.   
  28.   
  29.         static bool Contains(this string KeyString, char c)  
  30.         {  
  31.             return KeyString.IndexOf(c) >= 0;  
  32.         }   
  33.   
  34.   
  35.         private static int? TranslateNumber(char c)  
  36.         {  
  37.             if ("ABC".Contains(c))  
  38.                 return 2;  
  39.             else if ("DEF".Contains(c))  
  40.                 return 3;  
  41.             else if ("GHI".Contains(c))  
  42.                 return 4;  
  43.             else if ("JKL".Contains(c))  
  44.                 return 5;  
  45.             else if ("MNO".Contains(c))  
  46.                 return 6;  
  47.             else if ("PQR".Contains(c))  
  48.                 return 7;  
  49.             else if ("STUV".Contains(c))  
  50.                 return 8;  
  51.             else if ("WXYZ".Contains(c))  
  52.                 return 9;  
  53.             return null;  
  54.         }  
  55.     }  

In Call Translator class, at first, I wrote a static function with name ToNumber that takes a string as a parameter and the return type is also a string. The ToNumber function will check if the string row that we are passing as a parameter is null or White Spaces. If the function finds it null or WhiteSpace, then it returns an empty string; otherwise, it converts the string row to ToUpperInvariant.

After that, make an instance of String Builder with the name newNumber and check the string row. If the string row contains -0123456789 numbers, then this string is to Append. If  string consists of an alphabet, then this will call TranslateNumber function.

The TranslateNumber function converts alphabets to numbers.

Step 6

Initialize Buttons, EditText, String in MainActivity. Now, we require Find Controls and bind with buttons and EditText by ID, after this we need two Click Events and Setting their Functionality as shown below.

(File Name: MainActivity.cs)  

  1. using Android.App;  
  2. using Android.Widget;  
  3. using Android.OS;  
  4. using Android.Content;  
  5.   
  6. namespace PhoneWord  
  7. {  
  8.     [Activity(Label = "PhoneWord", MainLauncher = true, Icon = "@drawable/icon")]  
  9.     public class MainActivity : Activity  
  10.     {  
  11.         //Creating Buttons, EditText, String  
  12.   
  13.         private string translatedNumber = string.Empty;  
  14.         private EditText phoneNumberText;  
  15.         private Button translateButton, callButton;  
  16.   
  17.         protected override void OnCreate(Bundle bundle)  
  18.         {  
  19.             base.OnCreate(bundle);  
  20.   
  21.             SetContentView(Resource.Layout.Main);  
  22.   
  23.             //Find Controls  
  24.   
  25.             phoneNumberText = FindViewById<EditText>(Resource.Id.phoneNumberText);  
  26.             translateButton = FindViewById<Button>(Resource.Id.btnTranslate);  
  27.             callButton = FindViewById<Button>(Resource.Id.btnCall);  
  28.   
  29.             //Button Property and Events  
  30.             callButton.Enabled = false;  
  31.             translateButton.Click += TranslateButton_Click;  
  32.             callButton.Click += CallButton_Click;  
  33.         }  
  34.   
  35.         //On Translate Button Click Function  
  36.   
  37.         private void TranslateButton_Click(object sender, System.EventArgs e)  
  38.         {  
  39.             translatedNumber = CallTranslator.ToNumber(phoneNumberText.Text);  
  40.             if (string.IsNullOrWhiteSpace(translatedNumber))  
  41.             {  
  42.                 callButton.Text = "Call";  
  43.                 callButton.Enabled = false;  
  44.             }  
  45.             else  
  46.             {  
  47.                 callButton.Text = "Call" + translatedNumber;  
  48.                 callButton.Enabled = true;  
  49.             }  
  50.   
  51.         }  
  52.   
  53.         //On Call Button Click Function  
  54.         private void CallButton_Click(object sender, System.EventArgs e)  
  55.         {  
  56.             var callDialog = new AlertDialog.Builder(this);  
  57.             callDialog.SetMessage("Call" + translatedNumber + "?");  
  58.             callDialog.SetNeutralButton("Call", delegate  
  59.             {  
  60.                 var callIntent = new Intent(Intent.ActionCall);  
  61.                 callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber));  
  62.                 StartActivity(callIntent);  
  63.             });  
  64.   
  65.             callDialog.SetNegativeButton("Cancel", delegate { });  
  66.             callDialog.Show();  
  67.         }  
  68.   
  69.     }  
  70. }  

Output

Now, run the Application and you will see the output given below

Xamarin   Xamarin

Summary

Call Translator is a very simple application to learn very basic things. This was the process of creating Call Translator App in Xamarin.Android using Visual Studio. Please share your comments and feedback.

Up Next
    Ebook Download
    View all
    Learn
    View all