setError for TextInputLayout showing Error
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>
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&hl=en_US
Updated on July 09, 2022Comments
-
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 about 8 yearssetError don't work for textview. It works with editext only. To do it for textview follow this link stackoverflow.com/questions/13508270/…
-
Admin about 8 yearsits a TextInputLayout not textview
-
Gabriele Mariotti about 8 yearsWhat is tvFirstName ?
-
Admin about 8 yearstvFirstName is TextInputLayout
-
Ranjithkumar about 8 years@sreekanth r u try my answer?
-
Asha Antony about 7 yearsTry this stackoverflow.com/a/42779409/4960501
-
-
Admin about 8 yearsIt works me only change android:theme="@style/Theme.AppCompat" for TextInputLayout, used customize theme so it not working need to check that
-
Ranjithkumar about 8 yearsIf you need customize theme -> use the parent theme as Theme.AppCompat & write your customize theme.
-
Admin about 8 yearsthis 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 about 8 yearsuse 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 about 8 yearsHow to change floating label color in below 5 @Ranjith
-
Admin about 8 yearsLet us continue this discussion in chat.
-
Viktor Yakunin about 8 yearsHow 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 almost 7 yearsThis should be accepted answer. Only these steps worked for me.