I have a drawable like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="@drawable/seek_thumb_pressed" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="@drawable/seek_thumb_selected" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="@drawable/seek_thumb_selected" />
<item android:drawable="@drawable/seek_thumb_normal" />
In code, how do I set my Drawable’s specific state? I’d like to set it to the state_pressed=true state.
Got it. A comment from here helped: Android : How to update the selector(StateListDrawable) programmatically
So
Drawable.setState()takes an array in integers. These integers represent the state of the drawable. You can pass any ints you want. Once the ints pass in match a an “item” from the state list drawables the drawable draws in that state.It makes more sense in code:
Notice that my state list drawable has both the
state_pressed="true"andandroid:state_window_focused="true". So I have to pass both of those int values tosetState. When I want to clear it, I justminThumb.setState(new int[]{});