0

I am trying to implement a spell check for an editext in android studio. The framework shall validates the last word typed by the user. The problem is the spell checker framework is not returning suggestions and is not showing any error.

Here is an example of the output i am getting: ,,(2)

Codes below:

public class MainActivity extends AppCompatActivity implements 
SpellCheckerSession.SpellCheckerSessionListener {

EditText editText;
SpellCheckerSession mScs;
TextView tv1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    editText = (EditText) findViewById(R.id.user_message);
    tv1=findViewById(R.id.textView);
    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (!(editText.getText().toString().equalsIgnoreCase(""))) {
                String[] text = editText.getText().toString().split(" ");
                if (mScs != null) {
                    mScs.getSuggestions(new TextInfo(text[text.length-1]), 3); //last word
                } else {
                    // Show the message to user
                    Toast.makeText(getApplicationContext(), "Please turn on the spell checker from setting", Toast.LENGTH_LONG).show();
                    ComponentName componentToLaunch = new ComponentName("com.android.settings",
                            "com.android.settings.Settings$SpellCheckersSettingsActivity");
                    Intent intent = new Intent();
                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
                    intent.setComponent(componentToLaunch);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    try {
                        getApplicationContext().startActivity(intent);
                    } catch (ActivityNotFoundException e) {
                        // Error
                    }
                }

            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

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

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

public void onGetSuggestions(final SuggestionsInfo[] arg0) {
    final StringBuilder sb = new StringBuilder();
    Log.d("LENGTH", String.valueOf(arg0.length));

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

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

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

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

}

Thanks in advance.

Android
  • 1,420
  • 4
  • 13
  • 23

1 Answers1

0

Did you implement it in the Mainfest.xml and create the spellchecker.xml to specify the languages?

It is shown here in the Documentation: https://developer.android.com/guide/topics/text/spell-checker-framework

tobzilla90
  • 607
  • 5
  • 9
  • Yes created the xml file but i forgot to add the service tag to the manifest. Thanks to your answer i went to verify again. But i am not able to identify what to add to the android:name tag in the service tag as listed in the documentation android:name=".SampleSpellCheckerService". Could you please help me out with this? Thanks again – Samiirah Aujub Feb 09 '19 at 07:52
  • You need to put into the android:name the class name of your Java class where you extend the standard Andorid SpellCheckerClass. You can check this github repo where an example is shown: https://github.com/aosp-mirror/platform_development/tree/master/samples/SpellChecker/SampleSpellCheckerService – tobzilla90 Feb 09 '19 at 08:07
  • okay thanks. I will check it. I was following this tutorial [Link](https://www.tutorialspoint.com/android/android_spelling_checker.htm) and it was not mentioned anywhere to implement the spellchecker service. – Samiirah Aujub Feb 09 '19 at 08:20
  • One more query, do i need to define what the suggestions will show? – Samiirah Aujub Feb 09 '19 at 08:23