0

The EditText field has a TextChangedListener with setting/clearing the error in afterTextChanged() and with setting/clearing the error in (fragments) onCreateView().

I'm evaluating if the field has error when the Back button is pressed and it works like a charm ;)

Problem comes when the focus is on the field with keyboard open. Then, pressing back closes the keyboard which (apparently) removes the error from the EditText. Because of that, when error is evaluated on next back press, there is no error (and things happen which were not expected).

Is it possible that the layout change (caused by the soft keyboard) removes the error from the EditText?

When another (text field) is focused after the keyboard was closed, the error is there.

Btw. I have tried this solution, but the onKeyPreIme is never called...

Community
  • 1
  • 1
Saran
  • 3,845
  • 3
  • 37
  • 59

1 Answers1

0

This is what worked for me:

theEditText.addOnLayoutChangeListener(new OnLayoutChangeListener() {

  @Override
  public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft,
      int oldTop, int oldRight, int oldBottom) {
    Log.d(this.getClass().getSimpleName(), "onLayoutChange()");
    checkValidity(theEditText.getText()); // sets or clears the error on theEditText
  }
});

Mustn't set the listener on the root view as it gets called when laying out each element.

Caveat: works only on API 11 +.

Saran
  • 3,845
  • 3
  • 37
  • 59