Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change TextInputLayout hint color programmatically

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: enter image description here

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?

like image 997
antanas_sepikas Avatar asked May 24 '18 09:05

antanas_sepikas


1 Answers

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)
like image 98
Sergey Rusak Avatar answered Sep 22 '22 21:09

Sergey Rusak