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.
- using System.Text;
- using System;
- namespace Core
- {
- public static class PhonewordTranslator
- {
- 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 = TranslateToNumber(c);
- if (result != null)
- newNumber.Append(result);
- }
-
- }
- return newNumber.ToString();
- }
- static bool Contains (this string keyString, char c)
- {
- return keyString.IndexOf(c) >= 0;
- }
- static int? TranslateToNumber(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 ("PQRS".Contains(c))
- return 7;
- else if ("TUV".Contains(c))
- return 8;
- else if ("WXYZ".Contains(c))
- return 9;
- return null;
- }
- }
- }
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.
- EditText phoneNumberText = FindViewById<EditText>(Resource.Id.PhoneNumberText);
- Button translateButton = FindViewById<Button>(Resource.Id.TranslateButton);
- Button callButton = FindViewById<Button>(Resource.Id.CallButton);
Add the code given below for translate button under OnCreate method, place it after the last step.
-
- callButton.Enabled = false;
-
- string translatedNumber = string.Empty;
- translateButton.Click += (object sender, EventArgs e) => {
-
- translatedNumber = Core.PhonewordTranslator.ToNumber(phoneNumberText.Text);
- if (String.IsNullOrWhiteSpace(translatedNumber)) {
- callButton.Text = "Call";
- callButton.Enabled = false;
- } else {
- callButton.Text = "Call " + translatedNumber;
- callButton.Enabled = true;
- }
- };
As an overview, your code for MainActivity.cs is supposed to resemble the same, as given below.
- using System;
- using Android.App;
- using Android.Content;
- using Android.Runtime;
- using Android.Views;
- using Android.Widget;
- using Android.OS;
-
- namespace demoandroid
- {
- [Activity(Label = "demoandroid", MainLauncher = true, Icon = "@drawable/icon")]
- public class MainActivity : Activity
- {
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
-
-
-
- SetContentView(Resource.Layout.Main);
-
- EditText phoneNumberText = FindViewById<EditText>(Resource.Id.PhoneNumberText);
- Button translateButton = FindViewById<Button>(Resource.Id.TranslateButton);
- Button callButton = FindViewById<Button>(Resource.Id.CallButton);
-
-
- callButton.Enabled = false;
-
-
- string translatedNumber = string.Empty;
-
- translateButton.Click += (object sender, EventArgs e) =>
- {
-
- translatedNumber = Core.PhonewordTranslator.ToNumber(phoneNumberText.Text);
- if (String.IsNullOrWhiteSpace(translatedNumber))
- {
- callButton.Text = "Call";
- callButton.Enabled = false;
- }
- else
- {
- callButton.Text = "Call " + translatedNumber;
- callButton.Enabled = true;
- }
- };
- callButton.Click += (object sender, 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();
- };
- }
- }
- }
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
- Adding buttons in an Android app.
- Adding a new class to translate alphanumeric to numerics.
- Add the code for MainActivity.cs
- Set call permission for an Android app under Android Manifest.
- Build and run the program on your device.