EditText setError message does not clear after input

Viewed 66565

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?

7 Answers

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.

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

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
            }
        }
    }
}
Related