I am trying to change change TextInputLayout hint color programmatically. While settings
android:textColorHint="@color/redColor"
in xml works just fine and I get this: and while editing and that's what I want, but I need to set it dynamically
Now I TextInputLayout doesn't have setHintTextColor method but TextInputEditText does so I tried doing it like this:
editext.setHintTextColor(getColor(R.color.redColor))
But this doesn't work with editext which is child of TextInputLayout so even though TextInputLayout does support "android:textColorHint" attribute it doesn't support the "setHintTextColor" method I looked for ways to do it differently and I found people suggesting using "setHintTextAppearance" with predefined style, and that what I did, but it gives following result:
normal state: focused state:
Here's what style looks like
<style name="AppRedText" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/redColor</item>
<item name="android:textColorHint">@color/redColor</item>
<item name="android:textSize">12sp</item>
</style>
How can I achieve "android:textColorHint" attribute behaviour programmatically?
I have been looking for a solution for a long time. But finally found it! It turned out too easy)
Kotlin version:
private fun setTextInputLayoutHintColor(textInputLayout: TextInputLayout, context: Context, @ColorRes colorIdRes: Int) {
textInputLayout.defaultHintTextColor = ColorStateList.valueOf(ContextCompat.getColor(context, colorIdRes))
}
and finally:
setTextInputLayoutHintColor(textInputLayout, context, R.color.errorColor)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With