MichelReap
MichelReap

Reputation: 5770

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?

Upvotes: 15

Views: 17564

Answers (8)

Huma
Huma

Reputation: 11

We can manipulate the error as helper text because if you set an error it will definitely change the hint color, so I just used helperText instead of setError, here is an example

XML File

<com.google.android.material.textfield.TextInputLayout
                  android:id="@+id/textInputLayoutUserName"
                  android:layout_width="@dimen/view_0x"
                  android:layout_height="wrap_content"
                  style="@style/errorEnabledtextInputLayoutStyle">
    
                     <EditText
                           android:id="@+id/editTextUserName"
                           android:gravity="center_vertical"
                           android:background="@drawable/edit_text_background"
                           android:backgroundTint="@color/colorGray"
                           android:hint="@string/aap_ka_naam"
                           android:singleLine="true"
                           style="@style/standardTextViewStyle"
                           tools:targetApi="lollipop"
                           app:layout_constraintHorizontal_bias="0.0"/>
    
</com.google.android.material.textfield.TextInputLayout>

edit_text_background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:top="-3dp" android:left="-3dp" android:right="-3dp">
        <shape android:shape="rectangle">
               <stroke android:width="1dp" android:color="@color/colorWhite" />
               <padding android:bottom="50dp" android:top="10dp" android:right="10dp"android:left="5dp"/>
               <solid android:color="#00000000" />
        </shape>
     </item>
</layer-list>

Styles

<style name="errorEnabledtextInputLayoutStyle">
        <item name="hintTextColor">@color/colorGraySix</item>
        <item name="helperTextTextColor">@color/colorNegative</item>
        <item name="hintEnabled">true</item>
</style>

Function

fun TextInputLayout.setErrorText(error : String, editText: EditText) {
     this.helperText = error
     editText.backgroundTintList = ColorStateList.valueOf(
            ResourcesCompat.getColor(resources,
            R.color.colorNegative, null)
      )
}

Usage

textInputLayoutUserName.setErrorText(getString(R.string.name_error), editTextUserName)

Upvotes: 1

vikas kumar
vikas kumar

Reputation: 11018

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

Upvotes: 18

Rahil Ali
Rahil Ali

Reputation: 965

Here is an example:

    <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/email_layout"
                android:textColorHint="@color/gray"
                app:errorTextAppearance="@style/TextAppearence.App.TextInputLayout"
                app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout">

    <android.support.design.widget.TextInputEditText
                    android:id="@+id/et_email"
                    style="@style/TextStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:cursorVisible="true"
                    android:gravity="center|left|bottom"
                    android:hint="@string/email"
                    android:inputType="textEmailAddress"
                    android:maxLength="50"
                    android:paddingBottom="10dp"
                    android:textColor="@color/black_effective"
                    android:textSize="18sp" />

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

Style File:

   <style name="TextAppearence.App.TextInputLayout" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/gray</item>
    </style>

Snapshot:

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

Upvotes: 0

MeoMunm
MeoMunm

Reputation: 11

You can use the "helperTextTextColor" attribute to do this.

<com.google.android.material.textfield.TextInputLayout
            app:hintTextColor="@color/color_login_text_hint"
            app:helperTextTextColor="@color/error_color">
</com.google.android.material.textfield.TextInputLayout>

Upvotes: 0

Amin Turkzadeh
Amin Turkzadeh

Reputation: 21

Here is another method which you may would try

fun wrapInCustomStyle(myText: String): Spannable? {
val spannable = SpannableString(myText) Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

//Test 'Alignment.ALIGN_OPPOSIT' Too if not working

spannable.setSpan(

    AlignmentSpan.Standard(Layout.Alignment.ALIGN_NORMAL), 0,
    myText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
) 
return spannable

}

InputLayout.error=wrapInCustomStyle("sample")

Upvotes: 0

snersesyan
snersesyan

Reputation: 1677

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

implementation 'com.android.support:design:28.0.0'

Upvotes: 5

Mallikarjuna
Mallikarjuna

Reputation: 883

  1. TextInputLayout in Xml

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:id="@+id/tx_login_password"
            app:errorEnabled="true"
            style="@style/text_input_style"
            app:passwordToggleEnabled="true"
            app:hintTextAppearance="@style/text_Apearence"
            android:layout_below="@id/tx_login_username"
            android:layout_centerHorizontal="true"
            app:theme="@style/Theme.App.Base"
            android:layout_marginLeft="70dp"
            android:layout_marginStart="70dp"
            android:layout_marginRight="70dp"
            android:layout_marginEnd="70dp"
            app:errorTextAppearance="@style/text_Apearence"
            android:layout_height="wrap_content">
            <android.support.v7.widget.AppCompatEditText
                android:layout_width="match_parent"
                android:layout_marginTop="10dp"
                android:layout_height="wrap_content"
                android:hint="Password"
                android:paddingTop="5dp"
                android:typeface="sans"
                android:textStyle="bold"
                android:textSize="15sp"
                android:maxLines="1"
                android:inputType="textPassword"
                android:lines="1"
                android:elegantTextHeight="true"
                android:textAlignment="viewStart"
                android:layout_gravity="start"
                android:id="@+id/password"
                android:theme="@style/Theme.App.Base"
                app:theme="@style/Theme.App.Base"
                style="@style/EditText_style"
                />
        </android.support.design.widget.TextInputLayout>
    
  2. text_Apearence in style.xml file from resourses

<style name="text_Apearence" parent="TextAppearance.Design.Hint">
    <item name="android:textSize">15sp</item>
        <item name="textColor">@color/react_blue</item>
        <item name="android:colorActivatedHighlight">@color/react_blue</item>
        <item name="android:colorControlActivated">@color/react_blue</item>
    </style>
   

 1.  3. Style for Text Input Layout text_input_style XML file

<!-- begin snippet: js hide: false console: true babel: false -->

  1. Style for Text Input Layout text_input_style XML file

<style name="text_Apearence" parent="TextAppearance.Design.Hint">
    <item name="android:textSize">15sp</item>
        <item name="textColor">@color/react_blue</item>
        <item name="android:colorActivatedHighlight">@color/react_blue</item>
        <item name="android:colorControlActivated">@color/react_blue</item>
    </style>

Image Looks Like

Upvotes: 0

Mallikarjuna
Mallikarjuna

Reputation: 883

<style name="text_Apearence" parent="TextAppearance.Design.Hint">
    <item name="android:textSize">15sp</item>
        <item name="textColor">@color/react_blue</item>
        <item name="android:colorActivatedHighlight">@color/react_blue</item>
        <item name="android:colorControlActivated">@color/react_blue</item>
    </style>
   

 

Upvotes: -1

Related Questions