LevelListDrawable.javaAPI DocAndroid 1.5 API7472Wed May 06 22:42:00 BST


public class LevelListDrawable extends DrawableContainer
A resource that manages a number of alternate Drawables, each assigned a maximum numerical value. Setting the level value of the object with {@link #setLevel(int)} will load the image with the next greater or equal value assigned to its max attribute. A good example use of a LevelListDrawable would be a battery level indicator icon, with different images to indicate the current battery level.

It can be defined in an XML file with the <level-list> element. Each Drawable level is defined in a nested <item>. For example:

<level-list xmlns:android="">
<item android:maxLevel="0" android:drawable="@drawable/ic_wifi_signal_1" />
<item android:maxLevel="1" android:drawable="@drawable/ic_wifi_signal_2" />
<item android:maxLevel="2" android:drawable="@drawable/ic_wifi_signal_3" />
<item android:maxLevel="3" android:drawable="@drawable/ic_wifi_signal_4" />

With this XML saved into the res/drawable/ folder of the project, it can be referenced as the drawable for an {@link android.widget.ImageView}. The default image is the first in the list. It can then be changed to one of the other levels with {@link android.widget.ImageView#setImageLevel(int)}.

ref android.R.styleable#LevelListDrawableItem_minLevel
ref android.R.styleable#LevelListDrawableItem_maxLevel
ref android.R.styleable#LevelListDrawableItem_drawable

Fields Summary
private final LevelListState
private boolean
Constructors Summary
public LevelListDrawable()

private LevelListDrawable(LevelListState state)

        LevelListState as = new LevelListState(state, this);
        mLevelListState = as;
Methods Summary
public voidaddLevel(int low, int high, Drawable drawable)

        if (drawable != null) {
            mLevelListState.addLevel(low, high, drawable);
            // in case the new state matches our current state...
public voidinflate(android.content.res.Resources r, org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs)

        super.inflate(r, parser, attrs);

        int type;

        int low = 0;

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

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

            TypedArray a = r.obtainAttributes(attrs,

            low = a.getInt(
          , 0);
            int high = a.getInt(
          , 0);
            int drawableRes = a.getResourceId(
          , 0);


            if (high < 0) {
                throw new XmlPullParserException(parser.getPositionDescription()
                        + ": <item> tag requires a 'maxLevel' attribute");

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

            mLevelListState.addLevel(low, high, dr);

public Drawablemutate()

        if (!mMutated && super.mutate() == this) {
            mLevelListState.mLows = mLevelListState.mLows.clone();
            mLevelListState.mHighs = mLevelListState.mHighs.clone();
            mMutated = true;
        return this;
protected booleanonLevelChange(int level)

        int idx = mLevelListState.indexOfLevel(level);
        if (selectDrawable(idx)) {
            return true;
        return super.onLevelChange(level);