Johannes Staehlin
Johannes Staehlin

Reputation: 3720

EditText: Toggle InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS

I've created a custom EditText by extending android.widget.EditText.

I would like to have the auto correct spans only visible, when the EditText has focus. So I call setInputType(INPUT_NO_FOCUS); in the constructor and:

@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
    super.onFocusChanged(focused, direction, previouslyFocusedRect);
    if (focused) {
        setInputType(INPUT_FOCUS);
    } else {
        setInputType(INPUT_NO_FOCUS);
    }
}

with:

private final int INPUT_NO_FOCUS = TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE
        | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
private final int INPUT_FOCUS = TYPE_CLASS_TEXT | TYPE_TEXT_FLAG_MULTI_LINE
        | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT | InputType.TYPE_TEXT_FLAG_AUTO_COMPLET;

==> There is indeed no auto correction and auto complete, but it does not appear after clicking on the EditText. Input type is changed successfully.

How can I trigger the auto correction/ add auto completion?

//Edit:

User Case I want to archive:

  1. MyEditText extends EditText view is displayed and not focused. There is no spell checking on the text (--> No red spans/lines under the text)

  2. User clicks into the edit field to. --> Spell checking gets triggered --> Red lines appear (User can click on wrong spelled words and let the OS correct them)

  3. User clicks on another View element --> Red lines/spans disappear.

Upvotes: 4

Views: 1203

Answers (2)

Kirit  Vaghela
Kirit Vaghela

Reputation: 12674

Override this method in MyEditText class

@Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        setInputType(INPUT_FOCUS);
    }

Using this method you can change the inputType of EditText to INPUT_FOCUS that show red line under text when user edit text of MyEditText

Upvotes: 0

Gabe Sechan
Gabe Sechan

Reputation: 93678

I'm afraid there's no good way to do this. The problem is that the Android framework doesn't really have a mechanism to tell the keyboard when the input type has changed. A keyboard generally checks the type when the field opens. After that, if the type changes it won't know unless it polls for it, and I don't think any existing keyboard does that.

Upvotes: 2

Related Questions