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.
Drawable. invalidateSelf() will call the view's invalidateDrawable() method, which will invalidate only the current bounds of the drawable. If you aren't moving the drawable around, this will have the same effect as calling invalidate(mDrawable. getBounds()) .
In addition to simple drawing, Drawable provides a number of generic mechanisms for its client to interact with what is being drawn: The setBounds(Rect) method must be called to tell the Drawable where it is drawn and how large it should be.
Drawable-Selector Drawable selector is a list of background image states. It can be used as an image. The background changes according to the state of the component. File stored in /res/drawable/filename.xml. 1.
android:state_checked. State identifier indicating that the object is currently checked.
Don't have enough points to comment, so this is just an addition to what android-developer said. For certain states such as state_enabled, there is no obvious way to set the opposite value of that state (e.g. there is no state_disabled). To indicate a negative or opposite state, simply pass the negative value of the resource for that state.
int[] state = new int[] {-android.R.attr.state_enabled}; //Essentially state_disabled
Then pass that integer array into the setState() method as usual.
minThumb.setState(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:
int[] state = new int[] {android.R.attr.state_window_focused, android.R.attr.state_focused};
minThumb.setState(state);
Notice that my state list drawable has both the state_pressed="true"
and android:state_window_focused="true"
. So I have to pass both of those int values to setState
. When I want to clear it, I just minThumb.setState(new int[]{});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With