15

I've got a simple TextInputLayout containing an EditText View.

Now I wonder how to change the accent color (underline, hintTextColor when highlighted) programmatically. I can't seem to find a suitable method inside TextInputLayout.

Any suggestions? Thanks in advance.

user2410644
  • 3,861
  • 6
  • 39
  • 59

2 Answers2

19

IMHO InputTextLayout can not change label color programmatically, because it is set by style. I examined source code of InputTextLayout and wrote this hack helper method which create access to private color member:

public static void setInputTextLayoutColor(EditText editText, @ColorInt int color) {
    TextInputLayout til = (TextInputLayout) editText.getParent();
    try {
        Field fDefaultTextColor = TextInputLayout.class.getDeclaredField("mDefaultTextColor");
        fDefaultTextColor.setAccessible(true);
        fDefaultTextColor.set(til, new ColorStateList(new int[][]{{0}}, new int[]{ color }));

        Field fFocusedTextColor = TextInputLayout.class.getDeclaredField("mFocusedTextColor");
        fFocusedTextColor.setAccessible(true);
        fFocusedTextColor.set(til, new ColorStateList(new int[][]{{0}}, new int[]{ color }));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

mFocusedTextColor is used for set internal CollapsingTextHelper.mCollapsedTextColor which sets color of label.

Petr Daňa
  • 1,263
  • 12
  • 18
  • Thanks.. this helps us lot – shweta c Mar 08 '17 at 09:34
  • 1
    Can confirm, I've looked at the source and they are using styles and attributes, so you cannot do it programatically. This reflection-based approach is your best option. Other option would be to copy the class and add helper methods to set the colors programatically. – ashishduh Mar 09 '17 at 20:51
  • 1
    Be careful with this snippet. It assumes that the TextInputLayout will be the EditText's parent. This won't always be the case, sometimes their is an intermediate FrameLayout. – Graydyn Young Apr 05 '17 at 14:08
  • Works perfectly fine to change label color. Do you know how to change underline and cursor color? – muthuraj Jun 01 '17 at 09:23
  • 1
    @muthuraj for underline try call setSupportBackgroundTintList() method. For changing cursor color I use also trick with access to private members from https://stackoverflow.com/questions/7238450/set-edittext-cursor-color/32474978#32474978 – Petr Daňa Jun 01 '17 at 09:30
  • Thanks @PetrDaňa I also had to change the cursor droplet color which I did using https://stackoverflow.com/a/27307004/3423932 – muthuraj Jun 01 '17 at 11:05
  • FYI, setSupportBackgroundTintList is an internal support lib API...expect it to go away in a future version :-( – kenyee Jun 12 '17 at 15:55
  • `ViewCompat.setBackgroundTintList(editText)` works for underline of EditText that does not focused. If it is focused then it uses default color...how can I solve this? been searching for a while still cannot find the workaround. – Arst Jan 09 '18 at 12:02
  • 1
    This will unfortunately not work with version 28+ of the Support Library. – afollestad Jul 19 '18 at 19:59
  • This, like any other reflection, would result in unwanted results if the actual private field is changed in the library. Ever since the support library is moved to AndroidX, the private field has changed (surprise!) and this solution is not working anymore. – Yasin YILDIRIM Nov 22 '18 at 08:20
4

You can try this for the text,

InputTextLayout.getEditText().setHighlightColor(yourColor);
InputTextLayout.getEditText().setHintTextColor(yourColor);

and this for the line at the bottom of the EditText

Drawable background = InputTextLayout.getEditText().getBackground();
DrawableCompat.setTint(background, yourColor);
InputTextLayout.getEditText().setBackground(background);

Hope it works!

speeD.
  • 57
  • 2
  • 2
    @sud007 I try this also and I confirmed that doesn't work. You can try my solution (my answer in this question) which is worked for me. – Petr Daňa Jun 10 '16 at 11:02
  • This works for background color but does not work for hightlight & hint colors – Julius Nov 13 '18 at 21:40