1. Create a spelling checker service class
To create a spelling checker service, the first step is to create a spelling checker service class that extends android.service.textservice.SpellCheckerService.
For a working example of a spelling checker, you may want to take a look at the
2. Implement the required methods
Next, in your subclass of SpellCheckerService, implement the methods createSession() and onGetSuggestions(), as shown in the following code snippet:
In addition to implementing your subclass, you need to declare the spelling checker service in your manifest file. The declaration specifies the application, the service, and a metadata file that defines the Activity to use for controlling settings. Here’s an example:
Last, create a metadata file for your spelling checker to define the Activity to use for controlling spelling checker settings. The metadata file can also define subtypes for the spelling checker. Place the file in the location specified in the
element of the spelling checker declaration in the manifest file.
In the example below, the metadata file spellchecker.xml specifies the settings Activity as
For faster, more accurate spell-checking, Android 4.1 (Jelly Bean) introduces APIs that let clients pass multiple sentences to your spelling checker at once.
To support sentence-level checking for multiple sentences in a single call, just override and implement the methodonGetSentenceSuggestionsMultiple(), as shown below.
If you’d like to learn more about how to use the spelling checker APIs, take a look at these documents and samples:
Spell Suggestion @ Interesting Code |
To create a spelling checker service, the first step is to create a spelling checker service class that extends android.service.textservice.SpellCheckerService.
For a working example of a spelling checker, you may want to take a look at the
SampleSpellCheckerService
class in the SpellChecker sample app, available from the Samples download package in the Android SDK.2. Implement the required methods
Next, in your subclass of SpellCheckerService, implement the methods createSession() and onGetSuggestions(), as shown in the following code snippet:
@Override public Session createSession() { return new AndroidSpellCheckerSession(); } private static class AndroidSpellCheckerSession extends Session { @Override public SuggestionsInfo onGetSuggestions(TextInfo textInfo, int suggestionsLimit) { SuggestionsInfo suggestionsInfo; ... // look up suggestions for TextInfo return suggestionsInfo; } }
3. Register the spelling checker service in AndroidManifest.xml
In addition to implementing your subclass, you need to declare the spelling checker service in your manifest file. The declaration specifies the application, the service, and a metadata file that defines the Activity to use for controlling settings. Here’s an example:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.samplespellcheckerservice"> <application android:label="@string/app_name"> <service android:label="@string/app_name" android:name=".SampleSpellCheckerService" android:permission="android.permission.BIND_TEXT_SERVICE"> <intent-filter> <action android:name="android.service.textservice.SpellCheckerService" /> </intent-filter> <meta-data android:name="android.view.textservice.scs" android:resource="@xml/spellchecker" /> </service> </application> </manifest>
4. Create a metadata XML resource file
Last, create a metadata file for your spelling checker to define the Activity to use for controlling spelling checker settings. The metadata file can also define subtypes for the spelling checker. Place the file in the location specified in the
element of the spelling checker declaration in the manifest file.
In the example below, the metadata file spellchecker.xml specifies the settings Activity as
SpellCheckerSettingsActivity
and includes subtypes to define the locales that the spelling checker can handle.<spell-checker xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/spellchecker_name" android:settingsactivity="com.example.SpellCheckerSettingsActivity" /> <subtype android:label="@string/subtype_generic" android:subtypeLocale="en" /> </spell-checker>
Bonus points: Add batch processing of multiple sentences
For faster, more accurate spell-checking, Android 4.1 (Jelly Bean) introduces APIs that let clients pass multiple sentences to your spelling checker at once.
To support sentence-level checking for multiple sentences in a single call, just override and implement the methodonGetSentenceSuggestionsMultiple(), as shown below.
private static class AndroidSpellCheckerSession extends Session { @Override public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple( TextInfo[] textInfo, int suggestionsLimit) { SentenceSuggestionsInfo[] sentenceSuggestionsInfos; ... // look up suggestions for each TextInfo return sentenceSuggestionsInfos } }
Documents and samples
If you’d like to learn more about how to use the spelling checker APIs, take a look at these documents and samples:
- Spelling Checker Framework API Guide — a developer guide covering the Spelling Checker API for clients and services.
- SampleSpellCheckerService sample app — helps you get started with your spelling checker service.
- You can find the app at /samples/android-15/SpellChecker/SampleSpellCheckerService in the Samples download.
- HelloSpellChecker sample app — a basic app that uses a spelling checker.
- You can find the app at /samples/android-15/SpellChecker/HelloSpellChecker in the Samples download.