Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the TextInputLayout outline color (Or how to override a color in a theme/style)

Tags:

Previously I asked how to customize the outline color of a TextInputLayout. You can check the question in this LINK.

Declaring this color in my app:

<color name="mtrl_textinput_default_box_stroke_color" tools:override="true">#fff</color>

This works, but changes the line color of all the TextInputLayout in the app. How can I apply different colors to different TextInputLayouts within the same app?

Thanks

like image 279
Addev Avatar asked Jun 19 '18 12:06

Addev


1 Answers

Well you can always use ye olde reflection until Google figures out how we can access simple and rather basic stuff like this. The field in the TextInputLayout class is called defaultStrokeColor, so if you set it accessible and change the value then in the real world it should also change.

try {
    Field field = TextInputLayout.class.getDeclaredField("defaultStrokeColor");
    field.setAccessible(true);
    field.set(commentInputLayout,
        ContextCompat.getColor(itemView.getContext(), R.color.app_middleweight));
}
catch (NoSuchFieldException | IllegalAccessException e) {
    Log.w("TAG", "Failed to change box color, item might look wrong");
}
like image 170
Josttie Avatar answered Sep 28 '22 18:09

Josttie