Setting error state in EditText through styles

Viewed 913

I have an Edittext with below style

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/white" android:state_focused="true" />
    <item android:color="@color/yellow" android:state_focused="false" />
    <item android:color="@color/white" android:state_active="true" />
</selector>

And in the Edittext style I apply

<item name="backgroundTint">@drawable/states_edit_text</item>

and it works. This changes the EditTexts bottom line color in states normal and focused . I need to change the tint color to red when its in the error state.

Something like

<item android:state_error="true" android:color="@color/red"></item>

But there is no state called error, I referred to other answers and they recommend to achieve it through code. is there any way using android styles, I can set the EditText tint to red on error state?

1 Answers

I think you can change it programmatically :

if (!trueState){

ColorStateList colorStateList = ColorStateList.valueOf(color)
editText.setSupportBackgroundTintList(colorStateList)

}  

This will give the EditText the desired underline color.

Related