11

I wanted to change the color of hint in TEXT INPUT LAYOUT same when it is in focused or unfocused state.i have two TEXT INPUT LAYOUT fields.Both fields are same color.But it in focused state color appears but in an unfocused state the default grey color appears.i want to keep the color color of hint same in both focused and unfocused state.

   <com.example.viveka.snipeid.CustomTextInputLayout
        android:id="@+id/input_layout_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        app:errorEnabled="true"
        >
    <EditText
        android:id="@+id/editEmail"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:hint="email / snipe id"
        android:digits="abcdefghijklmnopqrstuvwxyz.@0123456789"
        android:textAllCaps="false"
        android:textSize="12sp"
        android:layout_marginLeft="-4dp"
        android:textColor="@color/lightgray"
        android:textColorHint="@color/primary"/>
    </com.example.viveka.snipeid.CustomTextInputLayout>

Expected image enter image description here what i got enter image description here

i need to change both fields as same color..

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
user8367573
  • 111
  • 1
  • 7

4 Answers4

4

you can simply do that by using the material TextInputLayout and add textColorHint , hintTextColor attributes , this works perfectly

  <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
          android:textColorHint="@color/gray"
        app:hintTextColor="@color/gray"
     
       >
Abdev
  • 301
  • 2
  • 7
1

You can do like this.

android:textColorHint="#FF0000" was EditText's hint color.

If you want the text color as well as the hint color.

You can change android:textColor="#FF0000" in the EditText.

You can add app:errorTextAppearance="@style/text_in_layout_error_hint_Style" to change the error text color.

<com.example.viveka.snipeid.CustomTextInputLayout
    android:id="@+id/input_layout_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:textColorHint="#FF0000"
    app:errorTextAppearance="@style/text_in_layout_error_hint_Style"
    app:hintTextAppearance="@style/text_in_layout_hint_Style"
    app:errorEnabled="true">

    <EditText
        android:id="@+id/editEmail"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginLeft="-4dp"
        android:digits="abcdefghijklmnopqrstuvwxyz.@0123456789"
        android:hint="email / snipe id"
        android:textAllCaps="false"
        android:textColor="#FF0000"
        android:textColorHint="@color/primary"
        android:textSize="12sp"/>
</com.example.viveka.snipeid.CustomTextInputLayout>

The LEFT-TOP hint color depended on app:hintTextAppearance="@style/text_in_layout_hint_Style".

And the style code:

<style name="text_in_layout_hint_Style">
    <item name="android:textColor">#FF0000</item>
    <item name="android:textSize">12sp</item>
</style>

Add error text style

 <style name="text_in_layout_error_hint_Style">
    <item name="android:textColor">#00ff00</item>
    <item name="android:textSize">12sp</item>
</style>

like this: enter image description here Hope to help you.

KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
  • @KeLiuye its perfect.but again i want to change color when error comes..the color will be changed to green.Example:if i click login without fill the both fields.the color returns to green in both disable and focused... – user8367573 Jul 31 '17 at 08:24
  • @user8367573 ,you can add ```app:errorTextAppearance="@style/text_in_layout_error_hint_Style"``` to change the error text color. – KeLiuyue Jul 31 '17 at 08:38
0

Create a selector_text_input_layout_hint_color.xml (for example)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorWhenFocus" android:state_focused="true"/>
    <item android:color="@color/colorWhenNotFocus" android:state_focused="false"/>
</selector>

Set in the Text Input Layout with the next attribute:

android:textColorHint="@color/selector_text_input_layout_hint_color"

Thanks to: https://medium.com/@fast3r/this-is-brilliant-martin-thank-you-very-much-6c4a43a65df4

That's all. It works for me ;)

Adrián Prieto
  • 177
  • 1
  • 4
0

In new material theming, the hint color is a tinted version of the colorOnSurface attribute when unfocused.

When focused it is the colorPrimary

Muhammad Ahmed AbuTalib
  • 4,080
  • 4
  • 36
  • 59