Skip to content

Most visited

Recently visited

navigation

LevelListDrawable

public class LevelListDrawable
extends DrawableContainer

java.lang.Object
   ↳ android.graphics.drawable.Drawable
     ↳ android.graphics.drawable.DrawableContainer
       ↳ android.graphics.drawable.LevelListDrawable


A resource that manages a number of alternate Drawables, each assigned a maximum numerical value. Setting the level value of the object with 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="http://schemas.android.com/apk/res/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" />
 </level-list>

With this XML saved into the res/drawable/ folder of the project, it can be referenced as the drawable for an ImageView. The default image is the first in the list. It can then be changed to one of the other levels with setImageLevel(int). For more information, see the guide to Drawable Resources.

Summary

XML attributes

android:drawable Reference to a drawable resource to use for the frame. 
android:maxLevel The maximum level allowed for this item. 
android:minLevel The minimum level allowed for this item. 

Public constructors

LevelListDrawable()

Public methods

void addLevel(int low, int high, Drawable drawable)
void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme)

Inflate this Drawable from an XML resource optionally styled by a theme.

Drawable mutate()

Make this drawable mutable.

Protected methods

boolean onLevelChange(int level)

Override this in your subclass to change appearance if you vary based on level.

void setConstantState(DrawableContainer.DrawableContainerState state)

Inherited methods

From class android.graphics.drawable.DrawableContainer
From class android.graphics.drawable.Drawable
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback

XML attributes

android:drawable

Reference to a drawable resource to use for the frame. If not given, the drawable must be defined by the first child tag.

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This corresponds to the global attribute resource symbol drawable.

android:maxLevel

The maximum level allowed for this item.

Must be an integer value, such as "100".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol maxLevel.

android:minLevel

The minimum level allowed for this item.

Must be an integer value, such as "100".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol minLevel.

Public constructors

LevelListDrawable

Added in API level 1
LevelListDrawable ()

Public methods

addLevel

Added in API level 1
void addLevel (int low, 
                int high, 
                Drawable drawable)

Parameters
low int
high int
drawable Drawable

inflate

Added in API level 21
void inflate (Resources r, 
                XmlPullParser parser, 
                AttributeSet attrs, 
                Resources.Theme theme)

Inflate this Drawable from an XML resource optionally styled by a theme. This can't be called more than once for each Drawable. Note that framework may have called this once to create the Drawable instance from XML resource.

Parameters
r Resources: Resources used to resolve attribute values
parser XmlPullParser: XML parser from which to inflate this Drawable
attrs AttributeSet: Base set of attribute values
theme Resources.Theme: Theme to apply, may be null
Throws
XmlPullParserException
IOException

mutate

Added in API level 3
Drawable mutate ()

Make this drawable mutable. This operation cannot be reversed. A mutable drawable is guaranteed to not share its state with any other drawable. This is especially useful when you need to modify properties of drawables loaded from resources. By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification. Calling this method on a mutable Drawable will have no effect.

Returns
Drawable This drawable.

Protected methods

onLevelChange

Added in API level 1
boolean onLevelChange (int level)

Override this in your subclass to change appearance if you vary based on level.

Parameters
level int
Returns
boolean Returns true if the level change has caused the appearance of the Drawable to change (that is, it needs to be drawn), else false if it looks the same and there is no need to redraw it since its last level.

setConstantState

Added in API level 1
void setConstantState (DrawableContainer.DrawableContainerState state)

Parameters
state DrawableContainer.DrawableContainerState
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.