andumava
andumava

Reputation: 61

Set TextInputLayout floating hint color as Outline color

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

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?

Upvotes: 0

Views: 230

Answers (2)

Developer Live
Developer Live

Reputation: 62

The TextInputLayout must contain a textColorHint attribute for getting the desired result

<com.google.android.material.textfield.TextInputLayout

                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/txt_email"
                    android:padding="5dp"
                    android:textColorHint="@color/white"
                    app:hintTextColor="@color/white">

Upvotes: 0

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363825

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

Upvotes: 0

Related Questions