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.
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