Home » Android » android – setError for TextInputLayout showing Error

android – setError for TextInputLayout showing Error

Posted by: admin June 15, 2020 Leave a comment

Questions:

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)
How to&Answers:

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

Answer:

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>

Answer:

Sytle theme for textinputlayout :

<style name="HintInputTextAppearance" parent="TextAppearance.AppCompat">
<item name="android:textColor">@color/input_floating_color</item>
<item name="android:textSize">22dp</item>
<item name="colorAccent">@color/input_floating_color</item>
</style>
<style name="InputTextAppearance" parent="TextAppearance.AppCompat">
<item name="android:textColor">#ff000000</item>
<item name="android:textColorHint">#ff000000</item>
<item name="android:textSize">24dp</item>
<item name="colorAccent">#ff000000</item>
</style>

<style name="ErrorTextAppearance" parent="TextAppearance.AppCompat">
<item name="android:textColor">#ffff0000</item>
<item name="android:textColorHint">#ffff0000</item>
<item name="android:textSize">18sp</item>
<item name="colorAccent">#ffff0000</item>
</style>

Use in Layout as :

<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:id="@+id/name_signup"
app:errorEnabled="true"
app:errorTextAppearance="@style/ErrorTextAppearance"
app:hintTextAppearance="@style/HintInputTextAppearance"
android:layout_marginLeft="@dimen/left_margin_login"
android:layout_marginRight="@dimen/right_margin_login"
android:layout_height="match_parent">
    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:textAppearance="@style/InputTextAppearance"
        android:layout_height="wrap_content"
        android:hint="Name" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:id="@+id/email_signup"
app:errorEnabled="true"
app:errorTextAppearance="@style/ErrorTextAppearance"
app:hintTextAppearance="@style/HintInputTextAppearance"
android:layout_marginLeft="@dimen/left_margin_login"
android:layout_marginRight="@dimen/right_margin_login"
android:layout_height="match_parent">
    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:textAppearance="@style/InputTextAppearance"
        android:layout_height="wrap_content"
        android:hint="Email" />
</android.support.design.widget.TextInputLayout>

Answer:

Solution by @Sevastyan works great for using xml layout.

However, if i have a TextInputLayout and a TextInputEditText which i created programmatically, and i did this:

tilCode.setErrorEnabled(true);
tilCode.setErrorTextAppearance(R.style.error);

I will get an error. But just:

tilCode.setErrorTextAppearance(R.style.error);

Works for me.

Answer:

Use this it never need Theme.AppCompat

<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/tv_metadata_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/tv_metadata_Details"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>

Answer:

Use TextUtils

if(TextUtils.isEmpty(etFirstName.getText().toString()))
{
   etFirstName.setError("Filed is mandatory!");
   view = etFirstName;
}

Answer:

Try below code to show error in TextInputLayout:

 tvFirstName.setErrorEnabled(true);
 tvFirstName.setError("Please Enter firstName");