color-list-resource.jd revision f940a1f316ddbed760f6f3ab9a3e4f2112909381
1page.title=Color State List Resource
2parent.title=Resource Types
3parent.link=available-resources.html
4@jd:body
5
6<div id="qv-wrapper">
7  <div id="qv">
8    <h2>See also</h2>
9    <ol>
10      <li><a href="more-resources.html#Color">Color (simple value)</a></li>
11    </ol>
12  </div>
13</div>
14
15
16<p>A {@link android.content.res.ColorStateList} is an object you can define in XML
17that you can apply as a color, but will actually change colors, depending on the state of
18the {@link android.view.View} object to which it is applied. For example, a {@link
19android.widget.Button} widget can exist in one of several different states (pressed, focused,
20or niether) and, using a color state list, you can provide a different color during each state.</p>
21
22<p>You can describe the state list in an XML file. Each color is defined in an {@code
23&lt;item>} element inside a single {@code &lt;selector>} element. Each {@code &lt;item>}
24uses various attributes to describe the state in which it should be used.</p>
25
26<p>During each state change, the state list is traversed top to bottom and the first item that
27matches the current state will be used&mdash;the selection is <em>not</em> based on the "best
28match," but simply the first item that meets the minimum criteria of the state.</p>
29
30<p class="note"><strong>Note:</strong> If you want to provide a static color resource, use a
31simple <a href="more-resources.html#Color">Color</a> value.</p>
32
33<dl class="xml">
34
35<dt>file location:</dt>
36<dd><code>res/color/<em>filename</em>.xml</code><br/>
37The filename will be used as the resource ID.</dd>
38
39<dt>compiled resource datatype:</dt>
40<dd>Resource pointer to a {@link android.content.res.ColorStateList}.</dd>
41
42<dt>resource reference:</dt>
43<dd>
44In Java: <code>R.color.<em>filename</em></code><br/>
45In XML: <code>@[<em>package</em>:]color/<em>filename</em></code>
46</dd>
47
48<dt>syntax:</dt>
49<dd>
50<pre class="stx">
51&lt;?xml version="1.0" encoding="utf-8"?>
52&lt;<a href="#selector-element">selector</a> xmlns:android="http://schemas.android.com/apk/res/android" >
53    &lt;<a href="#item-element">item</a>
54        android:color="<em>hex_color</em>"
55        android:state_pressed=["true" | "false"]
56        android:state_focused=["true" | "false"]
57        android:state_selected=["true" | "false"]
58        android:state_active=["true" | "false"]
59        android:state_checkable=["true" | "false"]
60        android:state_checked=["true" | "false"]
61        android:state_enabled=["true" | "false"]
62        android:state_window_focused=["true" | "false"] />
63&lt;/selector>
64</pre>
65</dd>
66
67<dt>elements:</dt>
68<dd>
69<dl class="tag-list">
70
71  <dt id="selector-element"><code>&lt;selector&gt;</code></dt>
72    <dd><strong>Required.</strong> This must be the root element. Contains one or more {@code
73&lt;item>} elements.
74      <p class="caps">attributes:</p>
75      <dl class="atn-list">
76        <dt><code>xmlns:android</code></dt>
77          <dd><em>String</em>. <strong>Required.</strong> Defines the XML namespace, which must be
78          <code>"http://schemas.android.com/apk/res/android"</code>.
79      </dl>
80    </dd>
81  <dt id="item-element"><code>&lt;item&gt;</code></dt>
82    <dd>Defines a color to use during certain states, as described by its attributes. Must be a
83child of a <code>&lt;selector&gt;</code> element.
84      <p class="caps">attributes:</p>
85      <dl class="atn-list">
86        <dt><code>android:color</code></dt>
87          <dd><em>Hexadeximal color</em>. <strong>Required</strong>. The color is specified with an
88RGB value and optional alpha channel.
89<p>The value always begins with a pound (#) character and then followed by the
90Alpha-Red-Green-Blue information in one of the following formats:</p>
91<ul>
92  <li>#<em>RGB</em></li>
93  <li>#<em>ARGB</em></li>
94  <li>#<em>RRGGBB</em></li>
95  <li>#<em>AARRGGBB</em></li>
96</ul></dd>
97        <dt><code>android:state_pressed</code></dt>
98          <dd><em>Boolean</em>. "true" if this item should be used when the object is pressed (such as when a button
99is touched/clicked); "false" if this item should be used in the default, non-pressed state.</dd>
100        <dt><code>android:state_focused</code></dt>
101          <dd><em>Boolean</em>. "true" if this item should be used when the object is focused (such as when a button
102is highlighted using the trackball/d-pad); "false" if this item should be used in the default,
103non-focused state.</dd>
104        <dt><code>android:state_selected</code></dt>
105          <dd><em>Boolean</em>. "true" if this item should be used when the object is selected (such as when a
106tab is opened); "false" if this item should be used when the object is not selected.</dd>
107        <dt><code>android:state_checkable</code></dt>
108          <dd><em>Boolean</em>. "true" if this item should be used when the object is checkable; "false" if this
109item should be used when the object is not checkable. (Only useful if the object can
110transition between a checkable and non-checkable widget.)</dd>
111        <dt><code>android:state_checked</code></dt>
112          <dd><em>Boolean</em>. "true" if this item should be used when the object is checked; "false" if it
113should be used when the object is un-checked.</dd>
114        <dt><code>android:state_enabled</code></dt>
115          <dd><em>Boolean</em>. "true" if this item should be used when the object is enabled (capable of
116receiving touch/click events); "false" if it should be used when the object is disabled.</dd>
117        <dt><code>android:state_window_focused</code></dt>
118          <dd><em>Boolean</em>. "true" if this item should be used when the application window has focus (the
119application is in the foreground), "false" if this item should be used when the application
120window does not have focus (for example, if the notification shade is pulled down or a dialog appears).</dd>
121      </dl>
122      <p class="note"><strong>Note:</strong> Remember that the first item in the state list that
123matches the current state of the object will be applied. So if the first item in the list contains
124none of the state attributes above, then it will be applied every time, which is why your
125default value should always be last (as demonstrated in the following example).</p>
126    </dd>
127
128</dl>
129</dd> <!-- end  elements and attributes -->
130
131<dt>example:</dt>
132<dd>XML file saved at <code>res/color/button_text.xml</code>:
133<pre>
134&lt;?xml version="1.0" encoding="utf-8"?>
135&lt;selector xmlns:android="http://schemas.android.com/apk/res/android">
136    &lt;item android:state_pressed="true"
137          android:color="#ffff0000"/> &lt;!-- pressed --&gt;
138    &lt;item android:state_focused="true"
139          android:color="#ff0000ff"/> &lt;!-- focused --&gt;
140    &lt;item android:color="#ff000000"/> &lt;!-- default --&gt;
141&lt;/selector>
142</pre>
143
144<p>This layout XML will apply the color list to a View:</p>
145<pre>
146&lt;Button
147    android:layout_width="fill_parent"
148    android:layout_height="wrap_content"
149    android:text="@string/button_text"
150    android:textColor="@color/button_text" />
151</pre>
152</dd> <!-- end example -->
153
154<dt>see also:</dt>
155<dd>
156<ul>
157  <li><a href="more-resources.html#Color">Color (simple value)</a></li>
158  <li>{@link android.content.res.ColorStateList}</li>
159  <li><a href="drawable-resource.html#StateList">State List Drawable</a></li>
160</ul>
161</dd>
162
163</dl>
164
165
166