public class

LevelListDrawable

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

Class Overview

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

Summary

XML Attributes
Attribute Name Related Method Description
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)
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.
[Expand]
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.

Related Methods

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.

Related Methods

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.

Related Methods

Public Constructors

public LevelListDrawable ()

Public Methods

public void addLevel (int low, int high, Drawable drawable)

public void inflate (Resources r, XmlPullParser parser, AttributeSet attrs)

public 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
  • This drawable.

Protected Methods

protected boolean onLevelChange (int level)

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

Returns
  • 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.