Search code examples
androidandroid-layoutmaterial-designandroid-textinputlayouttextinputlayout

Set TextInputLayout floating hint color as Outline color


I'm using MaterialComponents Outline TextInputLayout. I need to set different colors on hint texts:

  • "main" hint (when no text is present in the TextInput) --> black with 60% alpha
  • floating hint --> same color as the outline (that is, colorPrimary when inactive, colorAccent when active)

I'm using android:textColorHint to set main hint color and app:hintTextColor to set floating hint color. Alas this last attribute seems to work only if the TextInputLayout is active. If it's inactive, floating hint takes android:textColorHint color.

My current configuration

layout.xml

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/td_descriptionH_editText"
style="@style/Widget.App.TextInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/td_description">

<com.google.android.material.textfield.TextInputEditText
    android:id="@+id/td_description_editText"
    style="@style/Widget.App.TextInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

styles.xml

<style name="Widget.App.TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="android:textColorHint">@color/black_inactive</item>
    <item name="hintTextColor">?attr/drawableEditTextOutline</item>
    <item name="boxStrokeColor">?attr/drawableEditTextOutline</item>
</style>

drawableEditTextOutline.xml

<selector>
    <item android:color="@color/colorPrimary" android:state_focused="true"/>
    <item android:color="@color/colorAccent"/>
</selector>

TextInputLayout active is OK (floating hint is the same color as the outline)

active

TextInputLayout inactive without text is ok

inactive with text

TextInputLayout inactive with text is KO --> floating hint isn't the same color as the outline

inactive without text

How can I achieve this?


Solution

  • You can use something like:

    <style name="CustomOutlineBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        <item name="boxStrokeColor">@color/text_input_layout_stroke_color</item>
        <item name="android:textColorHint">@color/text_color_hint</item>
        <item name="hintTextColor">@color/green</item>
    </style>
    

    with the @color/text_color_hint selector:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:alpha="..." android:color="@color/red" android:state_enabled="false"/>
        <item android:alpha="..." android:color="@color/blue"/>
    </selector>
    

    and the @color/text_input_layout_stroke_color selector:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:alpha="..." android:color="@color/green" android:state_focused="true"/>
        <item android:alpha="..." android:color="@color/green" android:state_hovered="true"/>
        <item android:alpha="..." android:color="@color/red" android:state_enabled="false"/>
        <item android:alpha="..." android:color="@color/blue"/>  <!-- unfocused -->
    </selector>
    

    enter image description here