Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to specify background color in Color State List Resources?
    text
    copied!<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>I found example (sorry, URL no longer works) and 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 <code>AndroidManifest.xml</code> 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 <code>Test-Activity</code>:</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 <code>res/values/strings.xml</code>:</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><code>res/color/button_test_color.xml</code>:</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 <code>res/layout/main.xml</code> 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>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload