Creating Phoneword Android Project Using Xamarin Android Application

Introduction

Xamarin is a development platform that allows us to code native, cross-platform iOS, Android, and Windows Phone apps in C#.

Requirements

  1. Xamrain Studio or Visual Studio 
  2. Minimum 8 Gb ram
  3. Above i3 processor
  4. Fundamentals of C# programming language

Steps should be followed

Please follow my steps to create PhoneWord and use this tutorial effectively.

Step 1

Launch the Visual Studio 2017 and go to file new project.

Step 2

Then you need to select the Android template from the List and then name the application and press to create the Project and select where to save the project.

Step 3

Here we go to our new project that has been created and then we need to navigate to Designer Page. To open the designer page you need to open Solution Explorer ViewàSolution Explorer.



Step 4

Then to Open Designer Page we need to select the Following, Solution ExploreràResourcesàLayoutàMain.Xaml.



Step 5

Add this Following Code in Main.Xaml Source Page. This main.xaml file is our designer page and here our PhoneWord UI design will be present here. And if you can, do it by manually by dragging and dropping the TextBox and Buttons from the Toolbar.

  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.     android:minWidth="25px"  
  7.     android:minHeight="25px">  
  8.     <EditText  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:id="@+id/PhonewordText" />  
  12.     <Button  
  13.         android:text="Translate"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:id="@+id/TranslateButton" />  
  17.     <Button  
  18.         android:text="Call"  
  19.         android:layout_width="match_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:id="@+id/CallButton" />  
  22. </LinearLayout>  

Step 6

Add the Following Code in MainActivity.cs this main acitivity  plays a most important role and this makes our app work correctly and customizes buttons with background activity.

  1. using System;  
  2.   
  3. using Android.App;  
  4. using Android.Content;  
  5. using Android.Runtime;  
  6. using Android.Views;  
  7. using Android.Widget;  
  8. using Android.OS;  
  9.   
  10. namespace Phoneword.Droid  
  11. {  
  12.     [Activity (Label = "Phoneword.Droid", MainLauncher = true, Icon = "@drawable/icon")]  
  13.     public class MainActivity : Activity  
  14.     {  
  15.         EditText phoneNumberText;  
  16.         Button translateButton;  
  17.         Button callButton;  
  18.         string TranslatedNumber;  
  19.               
  20.   
  21.         protected override void OnCreate (Bundle bundle)  
  22.         {  
  23.             base.OnCreate(bundle);  
  24.   
  25.             // Set our view from the "main" layout resource  
  26.             SetContentView (Resource.Layout.Main);  
  27.   
  28.             // Get our button from the layout resource,  
  29.             // and attach an event to it  
  30.             Button button = FindViewById<Button> (Resource.Id.TranslateButton);  
  31.             SetContentView(Resource.Layout.Main);  
  32.             phoneNumberText = FindViewById<EditText>(Resource.Id.PhonewordText);  
  33.             translateButton = FindViewById<Button>(Resource.Id.TranslateButton);  
  34.             callButton = FindViewById<Button>(Resource.Id.CallButton);  
  35.   
  36.             translateButton.Click += TranslateButton_Click;  
  37.             callButton.Click += CallButton_Click;  
  38.   
  39.             }  
  40.     }  
  41. }   
Step 7

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

Then run the app using Visual Studio Android Emulator and here we go -- our app gets executed.

 

Output Of our PhoneWord,

 



Conclusion

Thank you for reading. Please make use of this article and if you have any doubts comment below and for any queries comment below. If you like it please follow me for more updates about Xamarin Android App development.


Similar Articles