6

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
antanas_sepikas
  • 5,644
  • 4
  • 36
  • 67

3 Answers3

21

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)
Sergey Rusak
  • 211
  • 3
  • 7
0

Please check the below link. Hope it may help you to give some idea.

[Change TextInputLayout accent color programmatically

TheHound.developer
  • 396
  • 1
  • 3
  • 16
  • I mentioned in the question that setHintTextAppearance doesn't work as expected – antanas_sepikas May 24 '18 at 10:03
  • Sorry. My mistake. Please check the other link. – TheHound.developer May 24 '18 at 10:07
  • I also mentioned that setHintTextColor doesn't work when EditText is child of TextInputLayout, which is suggested by the first answer in that post. As far as second answer goes it looks really scary using reflections for such a a thing. I Don't think that it's intended to be this way. What happens if "mDefaultTextColor" is renamed in the future? it's a private field and should not be accessed from outside the class – antanas_sepikas May 24 '18 at 10:12
0

I have found solution for this change TextInputLayout hint text color.

you can use like :

setUpperHintColor(ContextCompat.getColor(context,R.color.your_color_name));

private void setUpperHintColor(int color) {
        try {
            Field field = textInputLayout.getClass().getDeclaredField("mFocusedTextColor");
            field.setAccessible(true);
            int[][] states = new int[][]{
                    new int[]{}
            };
            int[] colors = new int[]{
                    color
            };
            ColorStateList myList = new ColorStateList(states, colors);
            field.set(textInputLayout, myList);

            Method method = textInputLayout.getClass().getDeclaredMethod("updateLabelState", boolean.class);
            method.setAccessible(true);
            method.invoke(textInputLayout, true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

And you need to set the backgroundTintList (or supportBackgroundTintList) on the EditText to an instance of ColorStateList containing only the color you wish to change the tint to. An easy way to do this in a backwards-compatible way looks like this:

ColorStateList colorStateList = ColorStateList.valueOf(ContextCompat.getColor(context,R.color.your_color_name))
editText.setSupportBackgroundTintList(colorStateList)

This will give the EditText the desired underline color.

Mayur Patel
  • 2,300
  • 11
  • 30