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)
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:text="Enter a Phone Number"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/textView" />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/phoneNumberText"
- android:text="+92300-Ahsan" />
- <Button
- android:text="Translate"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnTranslate" />
- <Button
- android:text="Call"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnCall" />
- </LinearLayout>
The designed from will look like below.
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)
- public static class CallTranslator
- {
- public static string ToNumber(string raw)
- {
- if (string.IsNullOrWhiteSpace(raw))
- return "";
- else
- {
- raw = raw.ToUpperInvariant();
- }
-
- var newNumber = new StringBuilder();
-
- foreach (var c in raw)
- {
- if (" -0123456789".Contains(c))
- newNumber.Append(c);
- else
- {
- var result = TranslateNumber(c);
- if (result != null)
- newNumber.Append(result);
- }
- }
- return newNumber.ToString();
- }
-
-
- static bool Contains(this string KeyString, char c)
- {
- return KeyString.IndexOf(c) >= 0;
- }
-
-
- private static int? TranslateNumber(char c)
- {
- if ("ABC".Contains(c))
- return 2;
- else if ("DEF".Contains(c))
- return 3;
- else if ("GHI".Contains(c))
- return 4;
- else if ("JKL".Contains(c))
- return 5;
- else if ("MNO".Contains(c))
- return 6;
- else if ("PQR".Contains(c))
- return 7;
- else if ("STUV".Contains(c))
- return 8;
- else if ("WXYZ".Contains(c))
- return 9;
- return null;
- }
- }
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)
- using Android.App;
- using Android.Widget;
- using Android.OS;
- using Android.Content;
-
- namespace PhoneWord
- {
- [Activity(Label = "PhoneWord", MainLauncher = true, Icon = "@drawable/icon")]
- public class MainActivity : Activity
- {
-
-
- private string translatedNumber = string.Empty;
- private EditText phoneNumberText;
- private Button translateButton, callButton;
-
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
-
- SetContentView(Resource.Layout.Main);
-
-
-
- phoneNumberText = FindViewById<EditText>(Resource.Id.phoneNumberText);
- translateButton = FindViewById<Button>(Resource.Id.btnTranslate);
- callButton = FindViewById<Button>(Resource.Id.btnCall);
-
-
- callButton.Enabled = false;
- translateButton.Click += TranslateButton_Click;
- callButton.Click += CallButton_Click;
- }
-
-
-
- private void TranslateButton_Click(object sender, System.EventArgs e)
- {
- translatedNumber = CallTranslator.ToNumber(phoneNumberText.Text);
- if (string.IsNullOrWhiteSpace(translatedNumber))
- {
- callButton.Text = "Call";
- callButton.Enabled = false;
- }
- else
- {
- callButton.Text = "Call" + translatedNumber;
- callButton.Enabled = true;
- }
-
- }
-
-
- private void CallButton_Click(object sender, System.EventArgs e)
- {
- var callDialog = new AlertDialog.Builder(this);
- callDialog.SetMessage("Call" + translatedNumber + "?");
- callDialog.SetNeutralButton("Call", delegate
- {
- var callIntent = new Intent(Intent.ActionCall);
- callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber));
- StartActivity(callIntent);
- });
-
- callDialog.SetNegativeButton("Cancel", delegate { });
- callDialog.Show();
- }
-
- }
- }
Output
Now, run the Application and you will see the output given below
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.