132

I am using an EditText widget, and I am validating it with the setError() method of EditText and it validates correctly.

But I have an button in the same screen that redirects to another activity. And when I press back button and come back to the screen the validation still appears.

So on the activity OnPause event I want to remove the validation of the EditText. How is it possible.

5 Answers 5

312
protected void onPause () {
    TextView textView = ...; // fetch it as appropriate
    textView.setError(null);
}

Because as mentioned in the documentation:

If the error is null, the error message and icon will be cleared.

1
  • 3
    Wow, so aside from NullPointerException, null value was really put into good use. LOL
    – ralphgabb
    Commented Nov 19, 2018 at 4:11
20

In Kotlin:

editText.error = null

Kotlin Extension Function:

To make it more readable, you could add this extension function

fun EditText.clearError() {
    error = null
}

In Java:

editText.setError(null);
1
  • in my case I have a material.textfield.TextInputEditText contained by a material.textfield.TextInputLayout. I had to set the error to null to the TextInputLayout to make it work. It won´t work if you do it with the internal TextInputEditText Commented yesterday
4

You can also do it using following :

protected void onPause () {    
    mEditText.setError(null);//removes error
    mEditText.clearFocus();    //clear focus from edittext
}
4

just put .setError(null) at the end of the EditText.

mEditText.setError(null);
0

In kotlin you can simply acces the property using property access syntax wich is

protected void onPause () {
    EditText mEditText = ...; // fetch it as appropriate
    mEditText.error = null
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.