I'm trying to use setError()
and use the maxLength
attribute on an EditText view, but after maxLength is reached and the user continues to type the error disappears. What is the proper place to call setError()
, so that it doesn't get removed in this situation?
XML code
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/myEditText"
android:maxLength="5"/>
Java Code
EditText myEditText = findViewById(R.id.myEditText);
myEditText.addTextChangedListener(new MyTextWatcher(myEditText));
Test TextWatcher that always sets error message.
private class MyTextWatcher implements TextWatcher {
private EditText view;
public MyTextWatcher(EditText editText) {view = editText;}
@Override public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {}
@Override public void onTextChanged(CharSequence charSequence, int start, int before, int count) {}
@Override public void afterTextChanged(Editable editable) {
view.setError("setError worked");
}
}
My testing device: Android 8.1 and software keyboard