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
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
- <?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">
- <EditText android:id="@+id/edtText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter your Text" />
- <Button android:id="@+id/btnSuggest" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Suggestion" />
- <TextView android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/txtSuggest" />
- </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
- using Android.App;
- using Android.Widget;
- using Android.OS;
- using Android.Views.TextService;
- using Android.Content;
- using static Android.Views.TextService.SpellCheckerSession;
- using System.Text;
- namespace SpellingChecker {
- [Activity(Label = "SpellingChecker", MainLauncher = true)]
- public class MainActivity: Activity, ISpellCheckerSessionListener {
- SpellCheckerSession spellChecker;
- TextView txtSuggest;
- public void OnGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {}
- public void OnGetSuggestions(SuggestionsInfo[] results) {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < results.Length; i++) {
-
- int len = results[0].SuggestionsCount;
- sb.Append('\n');
- for (int j = 0; j < len; j++) sb.Append(results[i].GetSuggestionAt(j) + " , ");
- sb.Append("(" + len + ")");
- }
- RunOnUiThread(delegate {
- txtSuggest.Append(sb.ToString());
- });
- }
- protected override void OnCreate(Bundle savedInstanceState) {
- base.OnCreate(savedInstanceState);
-
- SetContentView(Resource.Layout.Main);
- Button button = FindViewById < Button > (Resource.Id.btnSuggest);
- EditText editText = FindViewById < EditText > (Resource.Id.edtText);
- txtSuggest = FindViewById < TextView > (Resource.Id.txtSuggest);
- button.Click += delegate {
- Toast.MakeText(this, editText.Text.ToString(), ToastLength.Short).Show();
- spellChecker.GetSuggestions(new TextInfo(editText.Text.ToString()), 5);
- };
- }
- protected override void OnResume() {
- base.OnResume();
- TextServicesManager textServicesManager = (TextServicesManager) GetSystemService(Context.TextServicesManagerService);
- spellChecker = textServicesManager.NewSpellCheckerSession(null, null, this, true);
- }
- protected override void OnPause() {
- base.OnPause();
- if (spellChecker != null) spellChecker.Close();
- }
- }
- }
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.