setError for TextInputLayout showing Error

22,734

Solution 1

Give valid style for your TextInputLayout( android:theme="@style/Theme.AppCompat")

<android.support.design.widget.TextInputLayout
  android:id="@+id/testingInputLayout"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:theme="@style/Theme.AppCompat">

<EditText
   android:id="@+id/testingEditText"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:hint="@string/testText"
   android:inputType="textEmailAddress" />

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

You need to add appCompat & support design dependency (if not)

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'

In your manifest file add AppCompat theme,

<application
 ...
android:theme="@style/Theme.AppCompat">

Solution 2

Although I did not fully understand why the problem occurs, it must have to do something about how Android applies colors to Views according to its states.

Here is the way I solved the problem:

1) Define custom style, that will be used only for styling the errors (or hints) only:

In your res/values/styles.xml add new style node:

    <style name="error" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/colorAccent</item> <!--apply the color you wat here -->
        <item name="android:textSize">12dp</item>
    </style>

2) Apply the style to your TextInputLayout:

Also, make sure, that you specify errorEnabled attribute:

<android.support.design.widget.TextInputLayout
        android:id="@+id/input_email_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        app:errorEnabled="true"
        app:errorTextAppearance="@style/error"
    >
        <android.support.design.widget.TextInputEditText
            android:id="@+id/input_email"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:inputType="textEmailAddress"
                  android:hint="@string/hint_email"/>
    </android.support.design.widget.TextInputLayout>
Share:
22,734
Ranjithkumar
Author by

Ranjithkumar

Running on the programming world marathon 🏃‍♂️🏃‍♂️🏃‍♂️ Want to learn JetPack Compose? - try my blog https://www.jetpackcompose.net/ Want to learn Kotlin?? - try my Kotlin tutorial app https://play.google.com/store/apps/details?id=ranjih.kotlinandroid&amp;hl=en_US

Updated on July 09, 2022

Comments

  • Ranjithkumar
    Ranjithkumar almost 2 years

    TextInputLayout working fine, when tried to set Error it showing error.

    Code:

     if (etFirstName.getText().length() == 0) {
                etFirstName.requestFocus();
                tvFirstName.setError("Please enter firstname");
            } else {
                tvFirstName.setError(null);
                tvFirstName.setErrorEnabled(false);
            }
    

    Check Log:

    java.lang.RuntimeException: Failed to resolve attribute at index 24
                at android.content.res.TypedArray.getColor(TypedArray.java:401)
                at android.widget.TextView.<init>(TextView.java:692)
                at android.widget.TextView.<init>(TextView.java:629)
                at android.widget.TextView.<init>(TextView.java:625)
                at android.widget.TextView.<init>(TextView.java:621)
                at android.support.design.widget.TextInputLayout.setErrorEnabled(TextInputLayout.java:297)
                at android.support.design.widget.TextInputLayout.setError(TextInputLayout.java:344)
    
    • Vivek Mishra
      Vivek Mishra about 8 years
      setError don't work for textview. It works with editext only. To do it for textview follow this link stackoverflow.com/questions/13508270/…
    • Admin
      Admin about 8 years
      its a TextInputLayout not textview
    • Gabriele Mariotti
      Gabriele Mariotti about 8 years
      What is tvFirstName ?
    • Admin
      Admin about 8 years
      tvFirstName is TextInputLayout
    • Ranjithkumar
      Ranjithkumar about 8 years
      @sreekanth r u try my answer?
    • Asha Antony
      Asha Antony about 7 years
  • Admin
    Admin about 8 years
    It works me only change android:theme="@style/Theme.AppCompat" for TextInputLayout, used customize theme so it not working need to check that
  • Ranjithkumar
    Ranjithkumar about 8 years
    If you need customize theme -> use the parent theme as Theme.AppCompat & write your customize theme.
  • Admin
    Admin about 8 years
    this part working fine in V5 onwards, how can i show same things from V4.1 onwards....theme colours only not showing properly it showing diff in V4.1 and V5 how can i make same??
  • Ranjithkumar
    Ranjithkumar about 8 years
    use separate theme for 5 and above (values-v21 folder). below v5 use same values folder. see the answer for separate themes stackoverflow.com/a/32423780/3879847
  • Admin
    Admin about 8 years
    How to change floating label color in below 5 @Ranjith
  • Admin
    Admin about 8 years
  • Viktor Yakunin
    Viktor Yakunin about 8 years
    How this solve a problem? the editText.getText() has @NonNull which means that there is no need to check it for null, original code is valid!
  • Vlad
    Vlad almost 7 years
    This should be accepted answer. Only these steps worked for me.