Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextInputLayout hint color in error state

As per Googles Material Guidelines:

https://material.io/guidelines/components/text-fields.html#text-fields-layout

TextInputLayout hint should be the same color as the Error message:

enter image description here

However it is not like that and when I call setError("My error") only the underline and the error message show up in red.

How can I change this behavior to account for Google's own guidelines?

like image 976
MichelReap Avatar asked Oct 10 '17 13:10

MichelReap


People also ask

How do I show error in TextInputLayout?

Error Label The below xml code is from the activity_main. xml layout and has EditText fields for a default error label and a custom one. To display the error text, we'll have to call the method setError(String) on an instance of TextInputLayout in our MainActivity.

How do I get TextInputLayout from text?

TextInputLayout textInputCustomEndIcon = findViewById(R. id. editText); final TextInputEditText editText = new TextInputEditText(textInputCustomEndIcon. getContext()); textInputCustomEndIcon.


2 Answers

Here is how you can do it:

 <android.support.design.widget.TextInputLayout
        android:padding="8dp"
        android:id="@+id/tilSample"
        app:errorTextAppearance="@style/error_appearance"
        app:hintTextAppearance="@style/error_appearance"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/etSample"
            android:layout_margin="8dp"
            android:padding="8dp"
            android:hint="android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.TextInputLayout>

style.xml

<style name="error_appearance" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/colorRed</item>
        <item name="android:textSize">12sp</item>
    </style>

colors.xml

   <?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="colorRed">#f44336</color>
</resources>

EDIT

We can manipulate the error and hint colours in code also using:

tilSample.setErrorTextAppearance(R.style.error_appearance);  
tilSample.setHintTextAppearance(R.style.error_appearance);
like image 99
vikas kumar Avatar answered Oct 25 '22 12:10

vikas kumar


Now this is default behaviour. To achieve this, just update your support library version to 28+.

implementation 'com.android.support:design:28.0.0'
like image 5
snersesyan Avatar answered Oct 25 '22 12:10

snersesyan