English 中文(简体)
Android Basics

Android - User Interface

Android Advanced Concepts

Android Useful Examples

Android Useful Resources

Selected Reading

Android - Spelling Checker
  • 时间:2024-09-17

Android - Spelpng Checker


Previous Page Next Page  

The Android platform offers a spelpng checker framework that lets you implement and access spell checking in your apppcation.

In order to use spelpng checker , you need to implement SpellCheckerSessionListener interface and override its methods. Its syntax is given below −

pubpc class HelloSpellCheckerActivity extends Activity implements SpellCheckerSessionListener {
   @Override
   pubpc void onGetSuggestions(final SuggestionsInfo[] arg0) {
      // TODO Auto-generated method stub
   }
   
   @Override
   pubpc void onGetSentenceSuggestions(SentenceSuggestionsInfo[] arg0) {
      // TODO Auto-generated method stub
   }
}

Next thing you need to do is to create an object of SpellCheckerSession class. This object can be instantiated by calpng newSpellCheckerSession method of TextServicesManager class. This class handles interaction between apppcation and text services. You need to request system service to instantiate it. Its syntax is given below −

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

The last thing you need to do is to call getSuggestions method to get suggestion for any text, you want. The suggestions will be passed onto the onGetSuggestions method from where you can do whatever you want.

mScs.getSuggestions(new TextInfo(editText1.getText().toString()), 3);

This method takes two parameters. First parameter is the string in the form of Text Info object, and second parameter is the cookie number used to distinguish suggestions.

Apart from the the methods , there are other methods provided by the SpellCheckerSession class for better handpng suggestions. These methods are psted below −

Sr.No Method & description
1

cancel()

Cancel pending and running spell check tasks

2

close()

Finish this session and allow TextServicesManagerService to disconnect the bound spell checker

3

getSentenceSuggestions(TextInfo[] textInfos, int suggestionsLimit)

Get suggestions from the specified sentences

4

getSpellChecker()

Get the spell checker service info this spell checker session has.

5

isSessionDisconnected()

True if the connection to a text service of this session is disconnected and not apve.

Example

Here is an example demonstrating the use of Spell Checker. It creates a basic spell checking apppcation that allows you to write text and get suggestions.

To experiment with this example , you can run this on an actual device or in an emulator.

Steps Description
1 You will use Android studio to create an Android apppcation under a package com.example.sairamkrishna.myapppcation.
2 Modify src/MainActivity.java file to add necessary code.
3 Modify the res/layout/main to add respective XML components
4 Run the apppcation and choose a running android device and install the apppcation on it and verify the results

Following is the content of the modified main activity file src/MainActivity.java.

package com.example.sairamkrishna.myapppcation;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.textservice.TextInfo;
import android.view.textservice.TextServicesManager;

import android.widget.Button;
import android.widget.EditText;

import android.view.textservice.SentenceSuggestionsInfo;
import android.view.textservice.SpellCheckerSession;
import android.view.textservice.SpellCheckerSession.SpellCheckerSessionListener;
import android.view.textservice.SuggestionsInfo;

import android.widget.TextView;
import android.widget.Toast;

pubpc class MainActivity extends Activity implements SpellCheckerSessionListener  {
   Button b1;
   TextView tv1;
   EditText ed1;
   private SpellCheckerSession mScs;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      b1=(Button)findViewById(R.id.button);
      tv1=(TextView)findViewById(R.id.textView3);

      ed1=(EditText)findViewById(R.id.editText);
      b1.setOnCpckListener(new View.OnCpckListener() {
         @Override
         pubpc void onCpck(View v) {
            Toast.makeText(getApppcationContext(),
               ed1.getText().toString(),Toast.LENGTH_SHORT).show();
            mScs.getSuggestions(new TextInfo(ed1.getText().toString()), 3);
         }
      });
   }

   pubpc void onResume() {
      super.onResume();
      final TextServicesManager tsm = (TextServicesManager)
         getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);
      mScs = tsm.newSpellCheckerSession(null, null, this, true);
   }

   pubpc void onPause() {
      super.onPause();
      if (mScs != null) {
         mScs.close();
      }
   }

   pubpc void onGetSuggestions(final SuggestionsInfo[] arg0) {
      final StringBuilder sb = new StringBuilder();

      for (int i = 0; i < arg0.length; ++i) {
         // Returned suggestions are contained in SuggestionsInfo
         final int len = arg0[i].getSuggestionsCount();
         sb.append( 
 );

         for (int j = 0; j < len; ++j) {
            sb.append("," + arg0[i].getSuggestionAt(j));
         }

         sb.append(" (" + len + ")");
      }
		
      runOnUiThread(new Runnable() {
         pubpc void run() {
            tv1.append(sb.toString());
         }
      });
   }

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

Following is the modified content of the xml res/layout/main.xml.

In the following code abc indicates the logo of tutorialspoint.com
<?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" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
   
   <TextView android:text="Spell checker " android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textview"
      android:textSize="35dp"
      android:layout_apgnParentTop="true"
      android:layout_centerHorizontal="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point"
      android:id="@+id/textView"
      android:layout_below="@+id/textview"
      android:layout_centerHorizontal="true"
      android:textColor="#ff7aff24"
      android:textSize="35dp" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Suggestions"
      android:id="@+id/button"
      android:layout_apgnParentBottom="true"
      android:layout_centerHorizontal="true" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText"
      android:hint="Enter Text"
      android:layout_above="@+id/button"
      android:layout_marginBottom="56dp"
      android:focusable="true"
      android:textColorHighpght="#ff7eff15"
      android:textColorHint="#ffff25e6"
      android:layout_apgnRight="@+id/textview"
      android:layout_apgnEnd="@+id/textview"
      android:layout_apgnLeft="@+id/textview"
      android:layout_apgnStart="@+id/textview" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/abc"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Suggestions"
      android:id="@+id/textView3"
      android:textSize="25sp"
      android:layout_below="@+id/imageView" />

</RelativeLayout>

Following is the content of the res/values/string.xml.

<resources>
   <string name="app_name">My Apppcation</string>
</resources>

Following is the content of AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sairamkrishna.myapppcation" >
   
   <apppcation
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name=".MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      
   </apppcation>
</manifest>

Let s try to run ourr apppcation we just modified. I assume you had created your AVD while doing environment setup. To run the app from Android studio, open one of your project s activity files and cpck Run Ecppse Run Icon icon from the toolbar. Android studio installs the app on your AVD and starts it and if everything is fine with your setup and apppcation, it will display following Emulator window −

Android Spell Checker Tutorial

Now what you need to do is to enter any text in the field. For example, i have entered some text. Press the suggestions button. The following notification would appear in you AVD along with suggestions −

Android Spell Checker Tutorial

Now change the text and press the button again, pke i did. And this is what comes on screen.

Android Spell Checker Tutorial Advertisements