Xamarin.Android - Spelling Checker App

Introduction
 
In this article, I shall show you how to make a simple spell checker app in Xamarin Android. This spell checker app provides the facility to check correct and incorrect word. Spell Checker application is the best for learning English words. When you enter the wrong word, the spell checker gives you suggestion to correct that word. 
 
Remember that English is the most popular language to learn in this day and age, as it is the worldwide business language. This is one of the reasons why correct spellings are vitally important at all times – and it can be easily achieved with a quick spell check on your app!
 
For individuals who are not entirely confident of their English spelling, Spell Check – Check Spelling will help them gain confidence and improve their spelling skills. It is a great tool while dealing with the international clients or English native speakers, especially when English is not your first language. You won’t need to worry about making spelling mistakes and looking stupid or ill-informed in front of your friends and colleagues.
 
Prerequisites
  • Visual Studio 2017
The steps given below are required to be followed in order to create a Spell Checker app in Xamarin.Android, using Visual Studio.
 
Step 1
 
Open Visual Studio and go to New Project-> Templates-> Visual C#-> Android-> Blank app. Give it a name, like SpellingChecker.
 
 
 
Step 2
 
Open Solution Explorer-> Project Name-> Resources-> Layout-> Main.axml and add the following code. The layout will have an EditView in order to enter the input text. I also added a TextView to display the suggested text.
 
(FileName: Main.axml)
 
XML Code
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">  
  3.     <EditText android:id="@+id/edtText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter your Text" />  
  4.     <Button android:id="@+id/btnSuggest" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Suggestion" />  
  5.     <TextView android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/txtSuggest" />   
  6. </LinearLayout>  
Step 3
 
Now, go to solution Explorer-> Project Name-> MainActivity file and add the following code with appropriate namespaces.
 
(FileName: MainActivity.cs)
 
MainActivity C# Code
  1. using Android.App;  
  2. using Android.Widget;  
  3. using Android.OS;  
  4. using Android.Views.TextService;  
  5. using Android.Content;  
  6. using static Android.Views.TextService.SpellCheckerSession;  
  7. using System.Text;  
  8. namespace SpellingChecker {  
  9.     [Activity(Label = "SpellingChecker", MainLauncher = true)]  
  10.     public class MainActivity: Activity, ISpellCheckerSessionListener {  
  11.         SpellCheckerSession spellChecker;  
  12.         TextView txtSuggest;  
  13.         public void OnGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {}  
  14.         public void OnGetSuggestions(SuggestionsInfo[] results) {  
  15.             StringBuilder sb = new StringBuilder();  
  16.             for (int i = 0; i < results.Length; i++) {  
  17.                 //Returned suggestions are contained in SugesstionInfo  
  18.                 int len = results[0].SuggestionsCount;  
  19.                 sb.Append('\n');  
  20.                 for (int j = 0; j < len; j++) sb.Append(results[i].GetSuggestionAt(j) + " , ");  
  21.                 sb.Append("(" + len + ")");  
  22.             }  
  23.             RunOnUiThread(delegate {  
  24.                 txtSuggest.Append(sb.ToString());  
  25.             });  
  26.         }  
  27.         protected override void OnCreate(Bundle savedInstanceState) {  
  28.             base.OnCreate(savedInstanceState);  
  29.             // Set our view from the "main" layout resource  
  30.             SetContentView(Resource.Layout.Main);  
  31.             Button button = FindViewById < Button > (Resource.Id.btnSuggest);  
  32.             EditText editText = FindViewById < EditText > (Resource.Id.edtText);  
  33.             txtSuggest = FindViewById < TextView > (Resource.Id.txtSuggest);  
  34.             button.Click += delegate {  
  35.                 Toast.MakeText(this, editText.Text.ToString(), ToastLength.Short).Show();  
  36.                 spellChecker.GetSuggestions(new TextInfo(editText.Text.ToString()), 5);  
  37.             };  
  38.         }  
  39.         protected override void OnResume() {  
  40.             base.OnResume();  
  41.             TextServicesManager textServicesManager = (TextServicesManager) GetSystemService(Context.TextServicesManagerService);  
  42.             spellChecker = textServicesManager.NewSpellCheckerSession(nullnullthistrue);  
  43.         }  
  44.         protected override void OnPause() {  
  45.             base.OnPause();  
  46.             if (spellChecker != null) spellChecker.Close();  
  47.         }  
  48.     }  
  49. }  
Output
 
If you have Android Emulator, you can run it. After a few seconds, the app will start running on your Android Emulator.
 
 
 
Summary
 
This was the process of creating a Spelling Checker app in Xamarin.Android, using Visual Studio. Please share your comments and feedback.


Similar Articles