How to set TextInputLayout error message colour?
Asked Answered
H

11

100

How can I change the colour of the error message that can be set to appear below the text field in a TextInputLayout (via setError(...)see error state here)?

It normally shows as a red colour, which I want to change. Which item names/keys should I use in my styles.xml file to target the colour?


**Edit:**

Added app:errorTextAppearance key to my TextInputLayout:

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:id="@+id/welcome_current_week_container"
        app:errorTextAppearance="@style/WelcomeErrorAppearance">
        <EditText
            ..../>
    </android.support.design.widget.TextInputLayout>
</LinearLayout>

and the error appearance (set to green for testing):

<style name="WelcomeErrorAppearance" parent="@android:style/TextAppearance">
    <item name="android:textColor">@android:color/holo_green_dark</item>
</style>

The result is that the hint as well as the error message is coloured (screenshots from scaled Android Emulator):

Regular (no error):

Before Image

Error state:

After Image

Edit 2/Outcome:

When the error message appears, the hint above the field changes to the same colour as the error message, overriding hint colour – this is by design.

Higley answered 14/11, 2015 at 13:46 Comment(6)
Possible duplicate of How to write style to error text of EditText in android?Rapturous
The error color replaces hint color in error state. This is by design. See google.com/design/spec/components/… You can't work around this without altering the TextInputLayout class.Expansible
@EugenPechanec I didn't realise this was the case. Thanks for explainingHigley
@EugenPechanec I am pretty sure you are wrong here. The part you are referring is from character counter. For normal fields errors should look like on this image (notice that hint is not colored) material-design.storage.googleapis.com/publish/material_v_4/…Clinic
The specs are known to contain errors. The goal is to keep it consistent in one app now. So either color the error label everywhere or nowhere and youre golden.Expansible
@EugenPechanec code.google.com/p/android/issues/detail?id=195775 - it was error indeed and will be fixed in the future release :)Clinic
I
152

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

error example

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.

Inessential answered 14/11, 2015 at 14:21 Comment(14)
Thanks. Seems straightforward but couldn't find it for the life of me somehow!Higley
Update: Is it possible to just change the styling of the error message? This seems to change the hint style above the field too.Higley
Really? Not for me, look at the picture above. Are you sure you're assigning the style to app:errorTextAppearance?Inessential
Edited my main question with code and screenshots to show what's happeningHigley
@dabo You're probably misusing TextInputLayout. You're supposed to set android:hint on TextInputLayout instead of the EditText.Expansible
@dabo thanks for your answer – helpful in changing colour nonetheless, which is I what I wanted to do (so accepted it). Eugen explained that hint colour changing with error is by design, so my question ends here.Higley
@EugenPechanec I'm using a AutoCompleteTextView inside my TextInputLayout and it works great. Think I'm not misusing it, because that's how Google implement it in their template Login Activity.Inessential
@dabo Anything that extends EditText will be fine inside a TextInputLayout, that's not at all what I'm talking about. Did you set the android:hint attribute on the TextInputLayout or is the "Email address" label another TextView? That would explain why it doesn't take on the error color.Expansible
@EugenPechanec Simplified it looks like this: <android.support.design.widget.TextInputLayout app:errorTextAppearance="@style/error_appearance"><AutoCompleteTextView android:hint="@string/prompt_email"/></android.support.design.widget.TextInputLayout>. So yep, it's another TextView, that's why it doesn't take the error color from the TextInputLayout.Inessential
Doesn't work on 23.1.1, but works in 23.1.0! It is reported as bug and will eventually solved in next build.Varistor
If you want to only change the color, it's best that you set the style's parent to parent="TextAppearance.Design.Error". That way, it retains the default text size and any other attributes, but let's you customize specifically the error color, which was the goal of the question at hand.Extemporary
@Extemporary changing just the color is actually easier than that, as pointed out in my answer below.Electroencephalograph
@dabo Error:(29) No resource identifier found for attribute 'errorTextAppearance' in package 'android'Tun
Still Changing hint color tooCult
E
29

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>
Electroencephalograph answered 14/5, 2016 at 21:14 Comment(6)
Someone should really write a book about themes and styles and all the possible attributes. This is really insane, you can set color and style is so many ways, and there is no way to know which one is right way to go. And off course there is no documentation at all :( I mean there is but it's really confusing.Crept
this is much better answer than the higher scoring one !Ardeen
For me, this solution caused an error (textColorError not found), but I managed to set colorError attribute in my theme. Seems like every version of Android/Support Library has its own theme attributes.Disused
I'm getting 'Android resource linking failed', but with <item name="colorError">@color/error</item> works fineCompatible
Please update post.. textColorError is now colorError.Irons
Correct. Creating a theme with colorError and applying it directly to the material text input layout was the ONLY way I found to actually change the error text color, but a style for errorTextAppearance would only let me set font, size, and weight.Lamination
H
12

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>

enter image description here

Handsaw answered 22/10, 2019 at 7:12 Comment(1)
Thanks! I didn't know we should use errorTextColor directly in this style. I thought we needed to add errorTextAppearance and define a new style (with errorTextColor) there.Orizaba
T
7

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>
Teakettle answered 4/6, 2018 at 12:26 Comment(3)
What does your R.style.InputError_Purple look like?Simas
@toobsco42 style only defines text color. I've edited the answer with the actual implementation.Philender
Thank you! I use this view for both a valid text and error text (green and red) and the underline color not changing correctly was driving me crazy.Transhumance
D
6

I needed to do this dynamically. Using reflection:

public static void setErrorTextColor(TextInputLayout textInputLayout, int color) {
  try {
    Field fErrorView = TextInputLayout.class.getDeclaredField("mErrorView");
    fErrorView.setAccessible(true);
    TextView mErrorView = (TextView) fErrorView.get(textInputLayout);
    Field fCurTextColor = TextView.class.getDeclaredField("mCurTextColor");
    fCurTextColor.setAccessible(true);
    fCurTextColor.set(mErrorView, color);
  } catch (Exception e) {
    e.printStackTrace();
  }
}

You will need to call textInputLayout.setErrorEnabled(true) before invoking the above method for this to work.

Discourteous answered 12/1, 2016 at 19:52 Comment(2)
The error text color changes but the underline color still stays It changes only on the next call to the same functionHonor
@Dr.aNdRO This uses reflection, it isn't guaranteed to work always!Evaporate
R
1

UPDATE

Please use a custom view instead and not this


A modded version of @jared's Answer which works in my case :

public static void setErrorTextColor(TextInputLayout textInputLayout, int color) {
    try {
        Field fErrorView = TextInputLayout.class.getDeclaredField("mErrorView");
        fErrorView.setAccessible(true);
        TextView mErrorView = (TextView)fErrorView.get(textInputLayout);
        mErrorView.setTextColor(color);
        mErrorView.requestLayout();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Repeal answered 26/1, 2016 at 14:10 Comment(2)
The error text color changes but the underline color still stays It changes only on the next call to the same functionHonor
Dont use this. this is not reliable as of nowRepeal
N
0

If you are using com.google.android.material.textfield.TextInputLayout this input layout than you just set one style

<com.google.android.material.textfield.TextInputLayout
                        android:id="@+id/textInputLayoutPassword"
                        style="@style/LoginTextInputLayoutStyle"



<style name="LoginTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
        <item name="boxStrokeColor">@color/text_input_box</item>
        <item name="errorTextColor">@color/colorRed</item>
    </style>
Nickelodeon answered 27/5, 2019 at 6:17 Comment(0)
N
0

Depending on need, one can change/set TextInputLayout text color dynamically or directly in the layout XML file. Below is sample code snippets

Create a custom style which uses @android:style/TextAppearance as parent in your styles.xml file:

<style name="style_error_appearance" parent="@android:style/TextAppearance">
    <item name="android:textColor">@color/color_error</item>
    <item name="android:textSize">11sp</item>
</style>

And, use it in your TextInputLayout widget:

  1. Directly in XML Layout
 <android.support.design.widget.TextInputLayout
            android:id="@+id/your_input_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:errorTextAppearance="@style/style_error_appearance">
  1. Dynamically in your class
your_input_layout.setErrorTextAppearance(R.style.style_error_appearance);

If you want to set single/same error text color for your application then define the text color in your app theme

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Error text color... -->
    <item name="textColorError">@color/color_error</item>
    <!-- other styles... -->
</style>

And in your AndroidManifest.xml:

<application
    android:theme="@style/AppTheme"
    android:icon="@drawable/ic_launcher"
    android:label="@string/your_app_name">

    <!-- ... -->

</application>
Northrup answered 6/8, 2019 at 6:7 Comment(1)
textColorError doesn't exist.Orizaba
R
0

I set it from the code

til.setErrorTextColor(ColorStateList.valueOf(errorColor))
    
Rosana answered 30/10, 2023 at 12:21 Comment(0)
S
0

Add this properties to your app theme main style

<item name="errorIconTint">@color/error_red</item>
    <item name="errorTextColor">@color/error_red</item>
    <item name="colorError">@color/error_red</item>
    <item name="colorOnError">@color/error_red</item>
Selfsealing answered 3/4 at 20:21 Comment(0)
R
-2

I looked into the TextInputLayout source and I realised that error text color is gotten from colors.xml. Just add this to your colors.xml:

<color name="design_textinput_error_color_light" tools:override="true">your hex color</color>
Rufe answered 1/7, 2016 at 17:14 Comment(1)
Works for me with design library added.Rufe

© 2022 - 2024 — McMap. All rights reserved.