mVck
mVck

Reputation: 3030

Disable EditText auto correct while doing a replace

Is it possible to replace part of the EditText's text (usually the word the caret is on) with another word and skip the auto correct (and make sure the word added doesn't get corrected)?

I'm doing a "name mention" system that works well, using the EditText.getText().replace() method, but on some devices (ie. Galaxy S3 with default keyboard) the text I replace gets auto corrected when the user presses space...

I want to keep the auto correct for the rest of the EditText, but would like to disable it for the part I'm replacing.

Upvotes: 0

Views: 783

Answers (2)

Brad
Brad

Reputation: 21

I've has success with using InputMethodManager.html#restartInput(android.view.View) on the text view after performing a replace. I've tested it to work on the Samsung Keyboard on the Galaxy S8. To quote the docs, it seems designed for this problem:

You should call this when the text within your view changes outside of the normal input method or key input flow, such as when an application calls TextView.setText().

An example of its use:

editText.replace(0, 10, "foo");
InputMethodManager imm = (InputMethodManager) 
    context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.restartInput(editText);

Upvotes: 2

אביב פישר
אביב פישר

Reputation: 105

You can try changing the input type, like this:

editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

And after he press space, change it back, like this:

editText.setInputType(InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);

Upvotes: 2

Related Questions