Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextInputLayout: Set box stroke color programmatically (highlighted and unhighlighted)

I'm trying to use these material text fields with the box outline style. I see that there's a property called boxStrokeColor which lets me set the stroke only when the text field is highlighted when it's not highlighted it seems to pull that color from the global theme's colorPrimary value.

In our app the user can set their own background color for certain views and I'd like to change the stroke color of the text field to a suitable contrasting color.

Is there any clean way to set the unhighlighted box stroke color programmatically?

like image 302
TylerJames Avatar asked Jan 16 '19 17:01

TylerJames


1 Answers

You can use the setBoxStrokeColorStateList method. Something like:

textInputLayout.setBoxStrokeColorStateList(AppCompatResources.getColorStateList(this,R.color.text_input_layout_stroke_color));

It works with a selector as:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="?attr/colorPrimary" android:state_focused="true"/>
  <item android:alpha="0.87" android:color="?attr/colorOnSurface" android:state_hovered="true"/>
  <item android:alpha="0.12" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
  <item android:alpha="0.38" android:color="?attr/colorOnSurface"/>
</selector>
like image 139
Gabriele Mariotti Avatar answered Oct 09 '22 20:10

Gabriele Mariotti