Search code examples
androidmaterial-designandroid-stylesandroid-textinputlayout

Use custom hint text color for hints in enabled and disabled views


I'm trying to add custom styles to a TextInputLayout and TextInputEditText and I´m not able to get the expected results. What I need is to have a custom color "A" for hints in enabled views and a custom color "B" for hints in disabled views.

Right now I have a style with a selector for hints enabled/disabled. This is the style:

<style name="CustomTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
    <item name="boxStrokeColor">@color/input_blue</item>
    <item name="boxStrokeWidth">2dp</item>
    <item name="errorTextAppearance">@style/ErrorText</item>
    <item name="android:textColorHint">@color/selector_edithintcolor</item>
</style>

Here is the selector, you can see the color to make the point: enter image description here

Now some of the components in the layout:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/layout_text_disabled"
    style="@style/CustomTextInputLayoutStyle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="32dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/layout_phone_not_focused">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/txt_text_disabled"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_deactivated"
        android:inputType="text"
        android:text="@string/txt_disabled" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/layout_text_disabled_hint"
    style="@style/CustomTextInputLayoutStyle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="32dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/layout_text_disabled">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/txt_text_disabled_hint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_deactivated"
        android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>

Additionally I have a button to change the status of those 2 views from enabled to disabled, here is the behavior:

When views are enabled everything is as expected, hints have the green color as per the selector: Hints are showing green enabled color, which is OK

Now when I disable the views, this is what I get: Hints show the default color when disabled, not OK

Any ideas for making the disabled hints be orange as per the selector? Thanks in advance!


Solution

  • Following this link it appears that it is now possible with version 1.2.0-alpha03 of com.google.android.material:material You need to create a color state list like so :

    box_stroke_color_state_list.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="?colorPrimary" android:state_enabled="true" />
        <item android:color="?colorSecondary" android:state_hovered="true" />
        <item android:color="?colorSecondary" android:state_focused="true" />
        <!-- This is where the disable hint and box will take it's color -->
        <item android:alpha="@dimen/material_emphasis_disabled" android:color="@android:color/holo_green_dark" android:state_enabled="false" />
        <item android:color="@color/mtrl_textinput_default_box_stroke_color" />
    </selector>
    

    and then call it in your code :

    ContextCompat.getColorStateList(context, R.color.box_stroke_color_state_list)?.let {
        layout_text_disabled_hint.setBoxStrokeColorStateList(it)
    }
    

    enter image description here