1. Color State List Resource
A colorstatelist
is an object you can define in XML so can apply as a color, but would actually change Colors, depending on the State of
The View
object to which it is applied. For example, a Button widget can exist in one of several different states (pressed, focused, or
niether) and, using a color state list, you can provide a different the color during each state.
You can describe the "state" list in an XML file. Each color was defined <item>
in an element inside <selector>
a single element. Each <item>
uses
Various attributes to describe, the state of which it should be used.
2. Syntax
<?xml version="1.0"encoding="Utf-8"? ><selector xmlns:android="http://schemas.android.com/apk/res/android">//This must is the root element. Contains one or more <item> elements. <item//defines a color to use during certain States, as described by its attributesAndroid:color="Hex_color"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>
3. Example
//Res/color/button_color.xml<?xml version="1.0"encoding="Utf-8"? ><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"Android:color="#ffff0000"/> <!--pressed-<item android:state_focused="true"Android:color="#ff0000ff"/> <!--focused-<item android:color="#ff000000"/> <!--default--></selector>
// activity.xml<button android:layout_width="wrap_content" android:layout_height="wrap_content" Android:textcolor ="@color/button_color" android:text=" Button" />
3.APP resources-resource Types/color State List Resource