FileDocCategorySizeDatePackage
StateListDrawable.javaAPI DocAndroid 1.5 API9649Wed May 06 22:42:00 BST 2009android.graphics.drawable

StateListDrawable

public class StateListDrawable extends DrawableContainer
Lets you assign a number of graphic images to a single Drawable and swap out the visible item by a string ID value.

It can be defined in an XML file with the <selector> element. Each state Drawable is defined in a nested <item> element.

attr
ref android.R.styleable#StateListDrawable_visible
attr
ref android.R.styleable#StateListDrawable_variablePadding
attr
ref android.R.styleable#StateListDrawable_constantSize
attr
ref android.R.styleable#DrawableStates_state_focused
attr
ref android.R.styleable#DrawableStates_state_window_focused
attr
ref android.R.styleable#DrawableStates_state_enabled
attr
ref android.R.styleable#DrawableStates_state_checkable
attr
ref android.R.styleable#DrawableStates_state_checked
attr
ref android.R.styleable#DrawableStates_state_selected
attr
ref android.R.styleable#DrawableStates_state_active
attr
ref android.R.styleable#DrawableStates_state_single
attr
ref android.R.styleable#DrawableStates_state_first
attr
ref android.R.styleable#DrawableStates_state_middle
attr
ref android.R.styleable#DrawableStates_state_last
attr
ref android.R.styleable#DrawableStates_state_pressed

Fields Summary
private final StateListState
mStateListState
private boolean
mMutated
Constructors Summary
public StateListDrawable()

        this(null);
    
private StateListDrawable(StateListState state)

        StateListState as = new StateListState(state, this);
        mStateListState = as;
        setConstantState(as);
        onStateChange(getState());
    
Methods Summary
public voidaddState(int[] stateSet, Drawable drawable)
Add a new image/string ID to the set of images.

param
stateSet - An array of resource Ids to associate with the image. Switch to this image by calling setState().
param
drawable -The image to show.

        if (drawable != null) {
            mStateListState.addStateSet(stateSet, drawable);
            // in case the new state matches our current state...
            onStateChange(getState());
        }
    
public intgetStateCount()
Gets the number of states contained in this drawable.

return
The number of states contained in this drawable.
hide
pending API council
see
#getStateSet(int)
see
#getStateDrawable(int)

        return mStateListState.getChildCount();
    
public DrawablegetStateDrawable(int index)
Gets the drawable at an index.

param
index The index of the drawable.
return
The drawable at the index.
hide
pending API council
see
#getStateCount()
see
#getStateSet(int)

        return mStateListState.getChildren()[index];
    
public intgetStateDrawableIndex(int[] stateSet)
Gets the index of the drawable with the provided state set.

param
stateSet the state set to look up
return
the index of the provided state set, or -1 if not found
hide
pending API council
see
#getStateDrawable(int)
see
#getStateSet(int)

        return mStateListState.indexOfStateSet(stateSet);
    
android.graphics.drawable.StateListDrawable$StateListStategetStateListState()

        return mStateListState;
    
public int[]getStateSet(int index)
Gets the state set at an index.

param
index The index of the state set.
return
The state set at the index.
hide
pending API council
see
#getStateCount()
see
#getStateDrawable(int)

        return mStateListState.mStateSets[index];
    
public voidinflate(android.content.res.Resources r, org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs)


        TypedArray a = r.obtainAttributes(attrs,
                com.android.internal.R.styleable.StateListDrawable);

        super.inflateWithAttributes(r, parser, a,
                com.android.internal.R.styleable.StateListDrawable_visible);

        mStateListState.setVariablePadding(a.getBoolean(
                com.android.internal.R.styleable.StateListDrawable_variablePadding, false));
        mStateListState.setConstantSize(a.getBoolean(
                com.android.internal.R.styleable.StateListDrawable_constantSize, false));

        a.recycle();

        int type;

        final int innerDepth = parser.getDepth() + 1;
        int depth;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
                && ((depth = parser.getDepth()) >= innerDepth
                || type != XmlPullParser.END_TAG)) {
            if (type != XmlPullParser.START_TAG) {
                continue;
            }

            if (depth > innerDepth || !parser.getName().equals("item")) {
                continue;
            }

            int drawableRes = 0;

            int i;
            int j = 0;
            final int numAttrs = attrs.getAttributeCount();
            int[] states = new int[numAttrs];
            for (i = 0; i < numAttrs; i++) {
                final int stateResId = attrs.getAttributeNameResource(i);
                if (stateResId == 0) break;
                if (stateResId == com.android.internal.R.attr.drawable) {
                    drawableRes = attrs.getAttributeResourceValue(i, 0);
                } else {
                    states[j++] = attrs.getAttributeBooleanValue(i, false)
                            ? stateResId
                            : -stateResId;
                }
            }
            states = StateSet.trimStateSet(states, j);

            Drawable dr;
            if (drawableRes != 0) {
                dr = r.getDrawable(drawableRes);
            } else {
                while ((type = parser.next()) == XmlPullParser.TEXT) {
                }
                if (type != XmlPullParser.START_TAG) {
                    throw new XmlPullParserException(
                            parser.getPositionDescription()
                                    + ": <item> tag requires a 'drawable' attribute or "
                                    + "child tag defining a drawable");
                }
                dr = Drawable.createFromXmlInner(r, parser, attrs);
            }

            mStateListState.addStateSet(states, dr);
        }

        onStateChange(getState());
    
public booleanisStateful()

        return true;
    
public Drawablemutate()

        if (!mMutated && super.mutate() == this) {
            final int[][] sets = mStateListState.mStateSets;
            final int count = sets.length;
            mStateListState.mStateSets = new int[count][];
            for (int i = 0; i < count; i++) {
                mStateListState.mStateSets[i] = sets[i].clone();
            }
            mMutated = true;
        }
        return this;
    
protected booleanonStateChange(int[] stateSet)

        int idx = mStateListState.indexOfStateSet(stateSet);
        if (idx < 0) {
            idx = mStateListState.indexOfStateSet(StateSet.WILD_CARD);
        }
        if (selectDrawable(idx)) {
            return true;
        }
        return super.onStateChange(stateSet);