Spell Checker in Android example and Tutorial


Introduction :
              Android operating system provides spell checker framework that can be accessed across any application designed in android. Its very simple enough to implement , this tutorial explains spell checker with an simple example. Example is attached at the bottom for download.

Tutorial :
              To use Spell checker framework in our application first we need to implement  SpellCheckerSessionListener as shown below and override its method
(onGetSuggestions(), onGetSentenceSuggestions()). It is mandatory to override these methods, though you may not use it. Now lets go ahead and see how to implement Android Spell Checker example.

public class MainActivity extends Activity implements
  SpellCheckerSessionListener {
 public void onGetSuggestions(final SuggestionsInfo[] arg0) {

 }

 public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] arg0) {
  // TODO Auto-generated method stub
 }
}

Step 1:
                Next we need to create an object of SpellCheckerSession class by invoking  newSpellCheckerSession() method of TextServicesManager class. This class handles interaction between applications and text services. We can retrieve an instance of this interface as shown below.


final TextServicesManager tsm = (TextServicesManager) getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);
  scs = tsm.newSpellCheckerSession(null, null, this, true);

Step 2 :
                 Next this we need to invoke getSuggestions() method to check the spelling and retrieve appropriate suggestions as shown below :
scs.getSuggestions(new TextInfo(text.getText().toString()), 5);

This will in-turn invoke onGetSuggestions() method where in we can handle the retrieved suggestions to our wish. I have just looped through  the suggestions and appended them into a string builder for display purpose as shown below. This method takes two parameters, first one is the text for which we look for suggestions, next is the suggestion limit(count).

for (int i = 0; i < arg0.length; ++i)
  {
      final int len = arg0[i].getSuggestionsCount();
      sb.append('\n');
          for (int j = 0; j < len; ++j) 
             {
                sb.append(arg0[i].getSuggestionAt(j) + ",");
              }
   }
Then just display the string builder object, simple.. Thus Android spell checker framework makes it easier to implement text spell check ... Below is the output screenshot of this application.



activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/main"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:inputType="textMultiLine" >
    </EditText>

    <Button
        android:id="@+id/mainbtn"
        android:layout_width="150dip"
        android:layout_height="50dip"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="77dp"
        android:onClick="go"
        android:text="Suggest" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="73dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:text="Suggestions"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

MainActivity.java

package com.example.androidspellchecker;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.textservice.SentenceSuggestionsInfo;
import android.view.textservice.SpellCheckerSession;
import android.view.textservice.SpellCheckerSession.SpellCheckerSessionListener;
import android.view.textservice.SuggestionsInfo;
import android.view.textservice.TextInfo;
import android.view.textservice.TextServicesManager;
import android.widget.EditText;

@SuppressLint("NewApi")
public class MainActivity extends Activity implements
  SpellCheckerSessionListener {

 private SpellCheckerSession scs;
 private EditText text, suggestions;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  text = (EditText) findViewById(R.id.editText1);
  suggestions = (EditText) findViewById(R.id.editText2);
 }

 public void go(View view) {
  final TextServicesManager tsm = (TextServicesManager) getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);
  scs = tsm.newSpellCheckerSession(null, null, this, true);
  scs.getSuggestions(new TextInfo(text.getText().toString()), 3);
 }

 @Override
 public void onGetSuggestions(final SuggestionsInfo[] arg0) {
  final StringBuilder sb = new StringBuilder();
  for (int i = 0; i < arg0.length; ++i) {
   final int len = arg0[i].getSuggestionsCount();
   sb.append('\n');
   for (int j = 0; j < len; ++j) {
    sb.append(arg0[i].getSuggestionAt(j) + ",");
   }
  }
  suggestions.setText(sb.toString());
 }

 @Override
 public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] arg0) {
  // TODO Auto-generated method stub
 }

}
Source Code of this application
AndroidSpellChecker.zip




Reactions:

0 comments :

Post a Comment