Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextInputLayout error color not getting cleared?

I have a TextInputLayout with an EditText inside it.

This is my xml:

<android.support.design.widget.TextInputLayout
    android:id="@+id/textInputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Enter Text" />

</android.support.design.widget.TextInputLayout>

My java code:

((TextInputLayout) findViewById(R.id.textInputLayout)).setError("ERROR");

When I call setError("ERROR"), the and the label(hint) color and EditText's bottom line color gets changed to red and the error appears. This is the behaviour that I expect.

Now let's say I do not call setError(null) before destroying my activity. Now I open the same activity again. I can see that the bottom line remains red for all EditText fields inside in my application, although the label colour seems to be reset and the error message is dismissed. This is not always reproducible, but if I keep trying, I can eventually get it.

I am using a Nexus 4 with 5.1.1.

Am I doing something wrong?

like image 905
mUser1990 Avatar asked Dec 23 '15 10:12

mUser1990


1 Answers

This is due to a bug in the AppCompat library.

  • Issue 190829: TextInputLayout setError causes all TILs in the app to have red underline

Reported by [email protected], Oct 19, 2015 Using design support library 23.1.0

Steps to reproduce the problem (including sample code if appropriate).

  • SetError on one TIL (i.e. in a form)
  • The TIL has a red underline (ok)
  • Navigate back and enter the activity again. Or go to another Activity with TILs.

What happened.

  • All the TILs have a red underline, even in other Activities. (but no error text).
  • The red underlines disappear only after force closing the app.

Also reported here:

  • Issue 190355: TextInputLayout setError() will not show an error after it is cleared

Issue status was changed to FutureRelease on 11 Nov 2015, so we can hope for a fix coming soon.

In the meantime, it seems there are 3 workarounds:

  • compile with v23.0.1 of the library
    • but that introduces a similar issue where the error line does not reset
  • add an invisible EditText to the top of your layout
  • run the following code on your first EditText
    • editText.setBackground(editText.getBackground().getConstantState().newDrawable())
like image 185
Richard Le Mesurier Avatar answered Oct 04 '22 20:10

Richard Le Mesurier