Android: trouble with Color State List Resources: how to specify a background color?
<p>To give the user of my app an indication which field currently has the focus I am trying to change the background color of some of my fields depending on the current state, however, I am having troubles understanding Androids Color State List Resources:</p>

<p>There is this example at the bottom of <a href="http://developer.android.com/guide/topics/resources/color-list-resource.html">http://developer.android.com/guide/topics/resources/color-list-resource.html</a>. If I try <strong>exactly</strong> the same, i.e. if I want to adapt the <strong>textColor</strong> , things <strong>do</strong> work. However, if I try an only <strong>slightly different</strong> thing, namely to adapt the <strong>background</strong> color, things <strong>do not</strong> work and I don't understand why? Why is this so inconsistent???</p>

<p>To make it simpler to understand what I am trying to do, I append my misc. .xml files:</p>

<p>The AndroidManifest.xml file:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="mmo.android.test"
      android:versionCode="1"
      android:versionName="1.0"&gt;
    &lt;application android:icon="@drawable/icon" android:label="@string/app_name"&gt;
        &lt;activity android:name=".Test"
                  android:label="@string/app_name"&gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;
                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;
&lt;/manifest&gt; 
</code></pre>

<p>The Test-Activity:</p>

<pre><code>package mmo.android.test;

import android.app.Activity;
import android.os.Bundle;

public class Test extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
</code></pre>

<p>The res/values/strings.xml:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;resources&gt;
    &lt;string name="hello"&gt;Hello World, Test!&lt;/string&gt;
    &lt;string name="app_name"&gt;Test&lt;/string&gt;
&lt;/resources&gt;
</code></pre>

<p>res/color/button_test_color.xml:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    &lt;item android:state_pressed="true"   android:color="#f0f"/&gt; &lt;!-- pressed --&gt;
    &lt;item android:state_focused="true"   android:color="#ff0"/&gt; &lt;!-- focused --&gt;
    &lt;item android:color="#000"/&gt; &lt;!-- default --&gt;
&lt;/selector&gt;
</code></pre>

<p>and finally my res/layout/main.xml file:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    &gt;
    &lt;Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="foobar"
        android:textColor="@color/button_test_color"
        android:background="#f00"
     /&gt;
    &lt;!-- 
    &lt;Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="foobar"
        android:textColor="#f00"
        android:background="@color/button_test_color"
     /&gt;
     --&gt;
&lt;/LinearLayout&gt;
</code></pre>

<p>If I run this as shown here, it works, i.e. I get a button whose text color changes depending on whether the button is focuses, pressed, etc.</p>

<p>If I uncomment the lower button, where I just flipped the attribute values for textColor and background I get an exception, stating </p>

<pre><code>... &lt;item&gt; tag requires a 'drawable' attribute or child tag defining a drawable
</code></pre>

<p>What the heck am I missing here? Why is that color state list acceptable as a text color but not as a background color? How does one specify a view's background color depending on the view's state?</p>

<p>Michael</p>
