How to set TextInputLayout error message colour?
Actually, to change just the error message color, you can set textColorError
in your theme (and also set colorControlNormal
and colorControlActivated
for the general widget and hint text color). TextInputLayout
picks up that attribute. NOTE: if you set errorTextAppearance
to a custom style then textColorError
will have no effect.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">@color/control_normal</item>
<item name="colorControlActivated">@color/control_activated</item>
<item name="textColorError">@color/error</item>
<!-- other styles... -->
</style>
And in your AndroidManifest.xml:
<application
android:theme="@style/AppTheme"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<!-- ... -->
</application>
With the TextInputLayout
included in the Material Components Library just use the app:errorTextColor
attribute.
<com.google.android.material.textfield.TextInputLayout
app:errorTextColor="@color/...."
.../>
In a custom style you can use:
<style name="..." parent="Widget.MaterialComponents.TextInputLayout.FilledBox" >
<item name="errorTextColor">@color/...</item>
...
</style>
One side note. I have tried the accepted solution one with errorTextAppereance
.
It works really good but at first, the input underline color was not changing after applying a new errorTextAppereance
style. I see there are a few comments and that other people are experiencing the same issue.
In my case, this was happening when I was setting a new style after setting a new error text. Like this:
passwordInputLayout.error = "Password strength"
passwordInputLayout.setErrorTextAppearance(R.style.InputError_Purple)
After switching the order of this two methods the text and underline color changes as expected.
passwordInputLayout.setErrorTextAppearance(R.style.InputError_Purple)
passwordInputLayout.error = "Password strength"
And the error text appearance style looks something like this:
<style name="InputError" parent="TextAppearance.Design.Error"/>
<style name="InputError.Purple">
<item name="android:textColor">@color/purple</item>
</style>
Create a custom style which uses @android:style/TextAppearance
as parent in your styles.xml
file:
<style name="error_appearance" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/red_500</item>
<item name="android:textSize">12sp</item>
</style>
And use it in your TextInputLayout widget:
<android.support.design.widget.TextInputLayout
android:id="@+id/emailInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorTextAppearance="@style/error_appearance">
Edit: Set the hint on the object, which is inside your TextInputLayout (EditText
, TextView
, etc.) to hold different colors for the hint and the error.