[size=large][b]State List[/b][/size]
A [b][color=blue]StateListDrawable[/color][/b] is a drawable object defined in XML that uses a several different images to represent the same graphic, depending on the state of the object. For example, a Button widget can exist in one of several different states (pressed, focused, or niether) and, using a state list drawable, you can provide a different background image for each state.
You can describe the state list in an XML file. Each graphic is represented by an <item> element inside a single <selector> element. Each <item> uses various attributes to describe the state in which it should be used as the graphic for the drawable.
During each state change, the state list is traversed top to bottom and the first item that matches the current state is used—the selection is not based on the "best match," but simply the first item that meets the minimum criteria of the state.
file location:
[color=indigo][b]res/drawable/filename.xml[/b][/color]
The filename is used as the resource ID.
compiled resource datatype:
Resource pointer to a StateListDrawable.
resource reference:
In Java:[color=indigo][b] R.drawable.filename[/b][/color]
In XML: [color=indigo][b]@[package:]drawable/filename[/b][/color]
syntax:
<?xml version="1.0" encoding="utf-8"?>
[b][color=blue]<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize=["true" | "false"]
android:dither=["true" | "false"]
android:variablePadding=["true" | "false"] >
<item
android:drawable="@[package:]drawable/drawable_resource"
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_window_focused=["true" | "false"] />
</selector>[/color][/b]
[size=medium][i]elements:[/i][/size]
[color=indigo][b]<selector>[/b][/color]
Required. This must be the root element. Contains one or more <item> elements.
[i][size=medium]attributes[/size]:[/i]
[color=indigo][b]xmlns:android[/b][/color]
String. Required. Defines the XML namespace, which must be "http://schemas.android.com/apk/res/android".
[color=indigo][b]android:constantSize[/b][/color]
Boolean. "true" if the drawable's reported internal size remains constant as the state changes (the size is the maximum of all of the states); "false" if the size varies based on the current state. Default is false.
[color=indigo][b]android:dither[/b][/color]
Boolean. "true" to enable dithering of the bitmap if the bitmap does not have the same pixel configuration as the screen (for instance, an ARGB 8888 bitmap with an RGB 565 screen); "false" to disable dithering. Default is true.
[color=indigo][b]android:variablePadding[/b][/color]
Boolean. "true" if the drawable's padding should change based on the current state that is selected; "false" if the padding should stay the same (based on the maximum padding of all the states). Enabling this feature requires that you deal with performing layout when the state changes, which is often not supported. Default is false.
[color=indigo][b]<item>[/b][/color]
Defines a drawable to use during certain states, as described by its attributes. Must be a child of a <selector> element.
[i][size=medium]attributes:[/size][/i]
[color=indigo][b]android:drawable[/b][/color]
Drawable resource. Required. Reference to a drawable resource.
[color=indigo][b]android:state_pressed[/b][/color]
Boolean. "true" if this item should be used when the object is pressed (such as when a button is touched/clicked); "false" if this item should be used in the default, non-pressed state.
[color=indigo][b]android:state_focused[/b][/color]
Boolean. "true" if this item should be used when the object is focused (such as when a button is highlighted using the trackball/d-pad); "false" if this item should be used in the default, non-focused state.
[color=indigo][b]android:state_selected[/b][/color]
Boolean. "true" if this item should be used when the object is selected (such as when a tab is opened); "false" if this item should be used when the object is not selected.
[color=indigo][b]android:state_checkable[/b][/color]
Boolean. "true" if this item should be used when the object is checkable; "false" if this item should be used when the object is not checkable. (Only useful if the object can transition between a checkable and non-checkable widget.)
[color=indigo][b]android:state_checked[/b][/color]
Boolean. "true" if this item should be used when the object is checked; "false" if it should be used when the object is un-checked.
[color=indigo][b]android:state_enabled[/b][/color]
Boolean. "true" if this item should be used when the object is enabled (capable of receiving touch/click events); "false" if it should be used when the object is disabled.
[color=indigo][b]android:state_window_focused[/b][/color]
Boolean. "true" if this item should be used when the application window has focus (the application is in the foreground), "false" if this item should be used when the application window does not have focus (for example, if the notification shade is pulled down or a dialog appears).
Note: Remember that Android applies the first item in the state list that matches the current state of the object. So, if the first item in the list contains none of the state attributes above, then it is applied every time, which is why your default value should always be last (as demonstrated in the following example).
example:
XML file saved at res/drawable/button.xml:
[b][color=blue]<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/button_focused" /> <!-- focused -->
<item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>[/color][/b]
This layout XML applies the state list drawable to a Button:
[color=blue][b]<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/button" />[/b][/color]
A [b][color=blue]StateListDrawable[/color][/b] is a drawable object defined in XML that uses a several different images to represent the same graphic, depending on the state of the object. For example, a Button widget can exist in one of several different states (pressed, focused, or niether) and, using a state list drawable, you can provide a different background image for each state.
You can describe the state list in an XML file. Each graphic is represented by an <item> element inside a single <selector> element. Each <item> uses various attributes to describe the state in which it should be used as the graphic for the drawable.
During each state change, the state list is traversed top to bottom and the first item that matches the current state is used—the selection is not based on the "best match," but simply the first item that meets the minimum criteria of the state.
file location:
[color=indigo][b]res/drawable/filename.xml[/b][/color]
The filename is used as the resource ID.
compiled resource datatype:
Resource pointer to a StateListDrawable.
resource reference:
In Java:[color=indigo][b] R.drawable.filename[/b][/color]
In XML: [color=indigo][b]@[package:]drawable/filename[/b][/color]
syntax:
<?xml version="1.0" encoding="utf-8"?>
[b][color=blue]<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize=["true" | "false"]
android:dither=["true" | "false"]
android:variablePadding=["true" | "false"] >
<item
android:drawable="@[package:]drawable/drawable_resource"
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_window_focused=["true" | "false"] />
</selector>[/color][/b]
[size=medium][i]elements:[/i][/size]
[color=indigo][b]<selector>[/b][/color]
Required. This must be the root element. Contains one or more <item> elements.
[i][size=medium]attributes[/size]:[/i]
[color=indigo][b]xmlns:android[/b][/color]
String. Required. Defines the XML namespace, which must be "http://schemas.android.com/apk/res/android".
[color=indigo][b]android:constantSize[/b][/color]
Boolean. "true" if the drawable's reported internal size remains constant as the state changes (the size is the maximum of all of the states); "false" if the size varies based on the current state. Default is false.
[color=indigo][b]android:dither[/b][/color]
Boolean. "true" to enable dithering of the bitmap if the bitmap does not have the same pixel configuration as the screen (for instance, an ARGB 8888 bitmap with an RGB 565 screen); "false" to disable dithering. Default is true.
[color=indigo][b]android:variablePadding[/b][/color]
Boolean. "true" if the drawable's padding should change based on the current state that is selected; "false" if the padding should stay the same (based on the maximum padding of all the states). Enabling this feature requires that you deal with performing layout when the state changes, which is often not supported. Default is false.
[color=indigo][b]<item>[/b][/color]
Defines a drawable to use during certain states, as described by its attributes. Must be a child of a <selector> element.
[i][size=medium]attributes:[/size][/i]
[color=indigo][b]android:drawable[/b][/color]
Drawable resource. Required. Reference to a drawable resource.
[color=indigo][b]android:state_pressed[/b][/color]
Boolean. "true" if this item should be used when the object is pressed (such as when a button is touched/clicked); "false" if this item should be used in the default, non-pressed state.
[color=indigo][b]android:state_focused[/b][/color]
Boolean. "true" if this item should be used when the object is focused (such as when a button is highlighted using the trackball/d-pad); "false" if this item should be used in the default, non-focused state.
[color=indigo][b]android:state_selected[/b][/color]
Boolean. "true" if this item should be used when the object is selected (such as when a tab is opened); "false" if this item should be used when the object is not selected.
[color=indigo][b]android:state_checkable[/b][/color]
Boolean. "true" if this item should be used when the object is checkable; "false" if this item should be used when the object is not checkable. (Only useful if the object can transition between a checkable and non-checkable widget.)
[color=indigo][b]android:state_checked[/b][/color]
Boolean. "true" if this item should be used when the object is checked; "false" if it should be used when the object is un-checked.
[color=indigo][b]android:state_enabled[/b][/color]
Boolean. "true" if this item should be used when the object is enabled (capable of receiving touch/click events); "false" if it should be used when the object is disabled.
[color=indigo][b]android:state_window_focused[/b][/color]
Boolean. "true" if this item should be used when the application window has focus (the application is in the foreground), "false" if this item should be used when the application window does not have focus (for example, if the notification shade is pulled down or a dialog appears).
Note: Remember that Android applies the first item in the state list that matches the current state of the object. So, if the first item in the list contains none of the state attributes above, then it is applied every time, which is why your default value should always be last (as demonstrated in the following example).
example:
XML file saved at res/drawable/button.xml:
[b][color=blue]<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/button_focused" /> <!-- focused -->
<item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>[/color][/b]
This layout XML applies the state list drawable to a Button:
[color=blue][b]<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/button" />[/b][/color]