Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText setError message does not clear after input

Tags:

android

Ok so I only have a EditText field and a button, which when pressed triggers an AsyncTask.

EditText playerName = (EditText)findViewById(R.id.playerEditText);

if(playerName.getText().toString().length() == 0 )
    playerName.setError("Player name is required!");
else {
    // do async task
}

The problem is that the error message seems to stay up even after when I input valid text to search. Is there a way to remove the error as soon as the EditText is not empty?

like image 952
Michael Avatar asked Jul 24 '12 23:07

Michael


4 Answers

In your else bracket, put playerName.setError(null), which will clear the error.

like image 104
Alex Curran Avatar answered Nov 01 '22 08:11

Alex Curran


API documentation: "The icon and error message will be reset to null when any key events cause changes to the TextView's text." Though it is not so - and therefore we can regard this as bug.

If you use inputType such as textNoSuggestions, textEmailAddress, textPassword, the error is unset after a character is typed. Nearly as documented but again not exactly - when you delete a character, error stays. It seems, a simple workaround with addTextChangedListener and setError(null) can attain promised behavior.

Besides there are posts about icon losing on Android 4.2. So use with care.

like image 37
gints Avatar answered Nov 01 '22 08:11

gints


Try this listener:

      playerName.addTextChangedListener(new TextWatcher()
                {
                    public void afterTextChanged(Editable edt){
                        if( playerName.getText().length()>0)
                        {
                             playerName.setError(null);
                        }
                    }
like image 12
Jan Ziesse Avatar answered Nov 01 '22 06:11

Jan Ziesse


If you want to hide the error message one way is you apply onclicklistener on the edit box and then

editTextName.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            editTextName.setError(Null)
        }
    });
like image 3
ayush bagaria Avatar answered Nov 01 '22 08:11

ayush bagaria