Search code examples
androidandroid-edittextandroid-softkeyboardandroid-spellcheck

Android SpellChecker not working if keyboard is showing


I have a simple activity that use SpellCheckerSession to suggest similar match of a word. but if keyboard is showing spellChecker do not working. maybe its because of keyboard is using spell checker too.

my simplified activity is:

    public class TestActivity extends AppCompatActivity  implements  SpellCheckerSession.SpellCheckerSessionListener{

    private SpellCheckerSession spellChecker;

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

        test();

    }

    void test() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                spellChecker.getSentenceSuggestions(new TextInfo[] {new TextInfo("bool")}, 4);
                test();

            }
        }, 2000);
    }

    @Override
    public void onGetSuggestions(SuggestionsInfo[] results) {}

    @Override
    public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {

        Log.i("tag", "results size: " + results.length);
        for (SentenceSuggestionsInfo result : results) {
            final int len = result.getSuggestionsCount();

            for (int j = 0; j < len; ++j) {
                int a = result.getSuggestionsInfoAt(j).getSuggestionsCount();
                Log.i("tag", "getSuggestionsCount : " + a);

                for (int i = 0; i < a; ++i) {
                    Log.i("tag", "Suggestion: " + result.getSuggestionsInfoAt(j).getSuggestionAt(i));
                }
            }
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        final TextServicesManager tsm = (TextServicesManager)
                getApplicationContext().getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);
        if (tsm != null) {
            spellChecker = tsm.newSpellCheckerSession(null, Locale.US, this, true);

        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (spellChecker != null) {
            spellChecker.close();
        }
    }
}

in this activity I call getSentenceSuggestions every 2 second to test if it working or not. and in my activity layout I have a EditText. after lunching app spellChecker working and show suggestion in logcat but if a click on editText to show keyboaar, spellChecker don't work and getSuggestionsCount showing -1 in logcat. if hiding keyboard all things is working.


Solution

  • by adding android:inputType="textNoSuggestions|textVisiblePassword" to EditText solved this problem.

    another way is :

    EditText editText = findViewById(R.id.edit_text);
        editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
    

    if you using a library like search bar, and do not access to EditText view, maybe this working for you:

    EditText view = (EditText) getCurrentFocus();
                if (view != null) {
                    view.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
                }