EditText setError message does not clear after input

Android

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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.

Solution 3 - Android

Try this listener:

      playerName.addTextChangedListener(new TextWatcher()
                {
                    public void afterTextChanged(Editable edt){
                    	if( playerName.getText().length()>0)
				    	{
				        	 playerName.setError(null);
				    	}
                    }

Solution 4 - Android

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)
		}
	});

Solution 5 - Android

Below code worked for me

@OnTextChanged(
            value = R.id.editTextName,
            callback = OnTextChanged.Callback.TEXT_CHANGED)
    public void afterInput(CharSequence sequence) {
        editTextName.setError(null);
        editTextName.setErrorEnabled(false);
    }

'

> editTextName.setError(null) Will clear the error message. > > editTextName.setErrorEnabled(false) Will remove additional padding.

Solution 6 - Android

Add a TextWatcher to your EditText and onError, show your error message using et.setError(errorMessage) else you can remove the error message and error icon like below.

// to remove the error message in your EditText
et.setError(null);

// to remove the error icon from EditText.
et.setCompoundDrawables(null, null, null, null);

Solution 7 - Android

This code worked for me.

textInputSetting(binding.emailEdt)

fun textInputSetting(view: TextInputLayout) {
    view.apply {
        this.editText!!.addTextChangedListener {
            if (this.editText!!.text.isNotEmpty()) {
                this.error = null
                this.isErrorEnabled = false
            }
        }
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichaelView Question on Stackoverflow
Solution 1 - AndroidAlex CurranView Answer on Stackoverflow
Solution 2 - AndroidgintsView Answer on Stackoverflow
Solution 3 - AndroidJan ZiesseView Answer on Stackoverflow
Solution 4 - Androidayush bagariaView Answer on Stackoverflow
Solution 5 - AndroidRissmon SureshView Answer on Stackoverflow
Solution 6 - AndroidSureshCS50View Answer on Stackoverflow
Solution 7 - Androidnatchapols65View Answer on Stackoverflow