Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawable.setState() How do I control the specific state of the drawable?

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.

like image 340
user123321 Avatar asked Aug 13 '12 23:08

user123321


People also ask

How do you invalidate drawable?

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()) .

Which of the following method of the drawable class allow the client to interact with the drawn object?

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.

What is drawable selector?

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.

What is State_checked in Android?

android:state_checked. State identifier indicating that the object is currently checked.


2 Answers

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);
like image 45
nicholastmosher Avatar answered Oct 23 '22 22:10

nicholastmosher


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[]{});

like image 83
user123321 Avatar answered Oct 23 '22 21:10

user123321