How to set TextInputLayout error message colour?

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?

  • Creating ViewHolders for ListViews with different item layouts
  • Android Layout Design
  • Decoupling In-App-Billing from Activity
  • In playstore can we have two apps with same name with different package - Android
  • NullPointerException (@PhoneWindow:onKeyUpPanel:1002) {main}
  • Disabling the keyboard's send button when nothing has been typed yet
  • Thanks in advance.

    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.

    Related posts:

    Intent.ACTION_HEADSET_PLUG is received when activity starts
    Prevent repeating alarm from occurring on weekend
    Coloring Android Status Bar in Nav Drawer
    Upload large file in Android without outofmemory error
    Setting up TeamCity to build Android Studio project
    Android - Camera Preview
  • Detect touch press vs long press vs movement?
  • Roboelectric 3.0 testing fragments
  • How to Calculate Height of Android Phone from ground
  • Android Volley - How to isolate requests in another class
  • How do I set the rounded corner radius of a color drawable using xml?
  • Set color of TextView span in Android
  • 5 Solutions collect form web for “How to set TextInputLayout error message colour?”

    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.

    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>
    

    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.

    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();
        }
    }
    

    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>
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.