0

I am having an issue with the TextInputLayout error state. When I set the error on the TextInputLayout, then error message is red, along with the underline, but the hint does not (it stays the default colour - grey). I have played around with setting the errorTextAppearance, yet I cannot seem to get the hint text to change colour.

<android.support.design.widget.TextInputLayout
            android:id="@+id/textInputLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="My Hint"
            app:errorTextAppearance="@style/MyErrorTextAppearance">

<android.support.design.widget.TextInputEditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:maxLength="10"
                android:maxLines="1" />

styles.xml

<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="MyErrorTextAppearance" parent="TextAppearance.Design.Error">
    <item name="android:textColor">@color/colorError</item>
</style>

I set and clear the error as follows:

myTextInputLayout.setErrorEnabled(true);
myTextInputLayout.setError(errorMessage);

myTextInputLayout.setError(null);
myTextInputLayout.setErrorEnabled(false);
JPM
  • 1,482
  • 5
  • 18
  • 25

1 Answers1

0

try following code:

fun setHintColor(@ColorInt color: Int)
{
    try {
        var stateList = ColorStateList(arrayOf(intArrayOf(0)), intArrayOf(color))
        val defaultTextColorField = TextInputLayout::class.java.getDeclaredField("mDefaultTextColor")
        defaultTextColorField?.let {field->
            field.isAccessible = true
            field.set(this, stateList)
        }
        val focusedTextColorField = TextInputLayout::class.java.getDeclaredField("mFocusedTextColor")
        focusedTextColorField?.let{field->
            field.isAccessible = true
            field.set(this, stateList)
        }
    }catch(e: java.lang.Exception){
        // handle exception
    }
}
Wysel
  • 11
  • 1
  • Can you explain this code a little bit? How does it answer the question? – tshimkus Feb 28 '19 at 18:39
  • **mDefaultTextColor** is used internally by **TextInputLayout** for the hint and **mFocusedTextColor** is used for a label. When you call **setErrorTextAppearance** only **mFocusedTextColor** is affected, that's why you see label changing to your color, but only when edit field has focus. Unfortunately, to change the hint color you need to use reflection to access **mDefaultTextColor**. Hope this helps. – Wysel Mar 04 '19 at 19:42