TextInputLayout error color not getting cleared?
Asked Answered
C

4

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?

Cycling answered 23/12, 2015 at 10:58 Comment(2)
It should recreate the layout and I don't understand why are you getting red line even after destroying the activity. Can you post a little more code?Sucrose
Hey man have u solved this . Mine also the sameSovereignty
F
5

This is due to a bug in the AppCompat library.

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 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:

Flogging answered 13/1, 2016 at 16:43 Comment(0)
L
0

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

Lemmons answered 20/1, 2016 at 15:15 Comment(0)
G
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

Garvey answered 21/3, 2016 at 13:45 Comment(0)
C
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.

Condyloid answered 4/5, 2016 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.