LuckMan
LuckMan

Reputation: 95

How to change the Material Design TextInputLayout Hint Text Color?

I'm trying to set the hintTextColor AND the boxStrokeColor of Material Design's textInputLayout into 3 different state of colors, for example:

enter image description here

enter image description here

enter image description here

How can I accomplish this?

For the hintTextColor, I've tried the suggestion made by Gabriele Mariotti in here, but the problem is one of the colors is applied to two different states ([disabled] and [enabled but unfocused]), and I want to differentiate these two.

Upvotes: 1

Views: 870

Answers (1)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 365048

You can use a custom style:

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

Focused:

enter image description here

Unfocused:

enter image description here

Disabled:

enter image description here

Upvotes: 3

Related Questions