9

I am using TextInputLayout from com.android.support:design:23.3.0

When I first apply an error it is shown correctly.

mInputPassword.setError(getString(R.string.error_invalid_password));
mInputEmail.setError(getString(R.string.error_field_required));

enter image description here

On the next login attempt I clear the error.

mInputEmail.setError(null);
mInputPassword.setError(null);

Then I run the validation and set the error again using the same code as above but this time the red line is applied but the error text is missing. enter image description here

Anyone have any ideas on why this might be happening or have experienced similar situations?

I saw something similar reported here but it is for an older version of the design library and don't know if it is still a issue in the verison I am using,

J Whitfield
  • 755
  • 11
  • 22
  • 1
    have you tried to set `setErrorEnabled(true)` right before setting the error the second time? I know this should not be a acceptable behaviour of an Object, but maybe at some point it falls back to `false`..... – Opiatefuchs Apr 28 '16 at 11:04
  • otherwise, please show your gradle file.... – Opiatefuchs Apr 28 '16 at 11:07
  • I tried setting the error enabled to true before setting the error a second time and it had no effect. – J Whitfield Apr 28 '16 at 11:31
  • have you done the step setErrorEnabled(false) after setError(null) ? Try this and then setErrorEnabled(true) befor the next setError("error");.... – Opiatefuchs Apr 28 '16 at 11:38
  • setting setErrorEnabled(false) straight after setError(null) seems to have fixed the problem. If you would like to write this up as an answer I will accept it as a solution. – J Whitfield Apr 28 '16 at 11:48
  • glad that it works..... :) – Opiatefuchs Apr 28 '16 at 11:52

1 Answers1

14

You simply has to do these steps:

setErrorEnabled(true);
setError("error");

for clearing:

setError(null);
setErrorEnabled(false);

repeat the first step to set a new error. setError(null) clears the error message and icon, so I think the view to show is simply gone and setErrorEnabled(false) will reset it.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
  • 1
    Thank you for helping me with this issue. I think the setErrorEnabled(true) may be unnecessary as this is called inside of setError("yourString") – J Whitfield Apr 28 '16 at 11:54
  • Note: if you set `app:errorEnabled="true"` in your TextInputLayout in xml, it would inflate the space for error and there might be no need in using `setErrorEnabled()` programmatically. – Johnny Five Oct 24 '18 at 08:18