Implement Spell Checker Android
Hi everyone!
Spell check is of the most common programs used to identify words in a document that may not be spelled correctly. Today, one need not carry a dictionary to self spell check. The age of mobile technology has given rise to a number of applications that incorporate the spell checking functionality.
Android offers developers a spelling checker framework. In order to implement the spell checking feature, developers need to make use of the SpellCheckerSessionListener interface. The interface provides callbacks for getting results from text services. Through this post, we will learn how to implement a spell checker application in Android.
Pre-requisites: Android SDK, Eclipse IDE
Create a new Activity class called TestSpellCheckerActivity in any of your existing Android projects and add the following code!
TestSpellCheckerActivity.java
package com.app.test;
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;
import android.widget.TextView;
public class TestSpellCheckerActivity extends Activity implements SpellCheckerSessionListener {
private TextView txtSpellCheck;
private SpellCheckerSession btnChecker;
private EditText etWord;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spellcheck);
txtSpellCheck = (TextView)findViewById(R.id.txtLabel);
etWord = (EditText)findViewById(R.id.etWord);
}
@Override
public void onResume() {
super.onResume();
final TextServicesManager tsm = (TextServicesManager) getSystemService(
Context.TEXT_SERVICES_MANAGER_SERVICE);
btnChecker = tsm.newSpellCheckerSession(null, null, this, true);
}
@Override
public void onPause() {
super.onPause();
if (btnChecker != null) {
btnChecker.close();
}
}
@SuppressWarnings("deprecation")
public void getSuggestions(View view){
btnChecker.getSuggestions(new TextInfo(etWord.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));
}
}
runOnUiThread(new Runnable() {
public void run() {
txtSpellCheck.append(sb.toString());
}
});
}
@Override
public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] arg0) {
// TODO Auto-generated method stub
}
}
activity_spellcheck.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:id="@+id/etWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/instruction">
<requestFocus />
</EditText>
<Button
android:id="@+id/btnSuggest"
android:layout_width="150dip"
android:layout_height="50dip"
android:onClick="getSuggestions"
android:text="@string/spell_check"/>
<TextView
android:id="@+id/txtLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/suggestions"/>
</LinearLayout>
strings.xml
<string name="spell_check">Spell Check</string>
<string name="suggestions">Suggestions</string>
<string name="instruction">Enter any text</string>
Save all changes. Make sure no errors are present. Run the application on an Android device and you should see the following output!
Reference: Android SuggestionsInfo