6

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?

mUser1990
  • 291
  • 4
  • 9

4 Answers4

5

This is due to a bug in the AppCompat library.

Reported by elyess.a...@gmail.com, 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 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:

Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
0

This issue is solved on version 23.1.1 of the com.android.support: ... libraries

0

Had the same problem what worked for me was changing my theme to extend from Theme.Design.*. Source: Issue 202051: UnsupportedOperationException in TextInputLayout's counter

datiKaa
  • 326
  • 2
  • 15
0

As @Richard said, it is a bug. Issue 190829: TextInputLayout setError causes all TILs in the app to have red underline

I have used the solution of setting the constant state back to background. You can just extend the TextInputLayout with your own custom class where you override the setError() method:

public class CustomTextInputLayout extends TextInputLayout {

    // Constructors...

    @Override
    public void setError(@Nullable CharSequence error) {

        super.setError(error);
        if ((getEditText() != null && getEditText().getBackground() != null) &&
            (Build.VERSION.SDK_INT == 22 || Build.VERSION.SDK_INT == 21)) {
            Drawable drawable = getEditText().getBackground().getConstantState().newDrawable();
            getEditText().setBackgroundDrawable(drawable);
        }
    }
}

And then I am reusing this class for wrapping EditTexts. I didn't experience any side effects.

RenatoIvancic
  • 1,798
  • 3
  • 21
  • 36