0

I made a custom view which consists of two TextInputLayout

Here's the view:

The view

I set the underline color to blue through xml applying a selector to the boxStrokeColor of both ViewGroups. Both of them should change to red at some point, but when I change the same property programatically, only the one that is focused changes color.

fun setState(state: State) {
    val (color, visibility) = when (state) {
        State.NORMAL -> Pair(R.color.selector_key_input, View.INVISIBLE)
        State.ERROR -> Pair(R.color.selector_key_input_error, View.VISIBLE)
    }
    firstInputLayout.boxStrokeColor = ContextCompat.getColor(context, color)
    secondInputLayout.boxStrokeColor = ContextCompat.getColor(context, color)
    incorrectTextView.visibility = visibility
}

This seems to be the behavior Google has added to these components. Any ideas how to set the underline color programatically?

This is the red selector, and the blue is the same. The only difference is the blue is applied directly through xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:color="@color/red"/>
    <item android:state_hovered="true" android:color="@color/red"/>
    <item android:state_focused="true" android:color="@color/red"/>
    <item android:color="@color/red"/>
</selector>    
aboger
  • 2,214
  • 6
  • 33
  • 47
Serrot69
  • 51
  • 1
  • https://stackoverflow.com/questions/60898946/materialcomponents-textinputlayout-outlinedbox-it-doesnt-work-properly-boxbackg/60910419#60910419 – MMG Apr 20 '20 at 05:54
  • That post is about the hint color and the background, not the underline. My text fields are "Filled" and not "Outlined" – Serrot69 Apr 20 '20 at 06:11
  • In your selector put in the order `android:state_focused="true"`,`android:state_hovered="true"`,`android:state_enabled="false"` and the default state. – Gabriele Mariotti Apr 22 '20 at 14:14

0 Answers0