Search code examples
javaandroidvalidationandroid-edittext

How to manually validate EditText?


I have one EditText which is unEditable for user. I'm taking value to EditText by Calendar object. So when user submit form without selecting time it will show message to select time.

etTime.setError("Select Time");

Now user selects date from Calendar.

etTime.setText(functionWhichReturnsSelectedTime());

I'm talking about that Red Alert Sign which appears on side of EditText!!!

But the error message doesn't get away. The listener only listens to textChanged() of EditText and checks is EditText is empty or not? But I'm not providing user to access editability of EditText. So error is not getting away.

So how can I manually do that?


Solution

  • If you want to remove the error indicator on your edit text, you must call:

    etTime.setError(null);
    

    Also, it would be a good idea, to override afterTextChanged() method, and you can perform validation, before removing the error.