Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change TextInputLayout accent color programmatically

Tags:

android

colors

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.

like image 692
user2410644 Avatar asked Sep 07 '15 14:09

user2410644


People also ask

How do I set TextInputLayout style programmatically?

Method 1 - Create TextInputLayout programmatically with wrapping Context with android. view. ContextThemeWrapper and use. TextInputLayout layout = new TextInputLayout(new ContextThemeWrapper(getContext(), R.

How do I get TextInputLayout from text?

TextInputLayout textInputCustomEndIcon = findViewById(R. id. editText); final TextInputEditText editText = new TextInputEditText(textInputCustomEndIcon. getContext()); textInputCustomEndIcon.


1 Answers

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.

like image 57
Petr Daňa Avatar answered Sep 22 '22 09:09

Petr Daňa