views:

491

answers:

2

I followed this tutorial to create a color state list for a particular Android view. I just want it to highlight when clicked so the user knows why the screen just changed.

When the view is rendered, I get the following error:

org.xmlpull.v1.XmlPullParserException: Binary XML file line #3: tag requires a 'drawable' attribute or child tag defining a drawable

My color XML (in res/color/viewcolor.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <item android:state_pressed="true" android:color="#ff33ffff"/> <!-- pressed -->
    <item android:color="#ff000000"/> <!-- default -->
</selector>

My layout XML (in res/layout/myview.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/myview"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="top"
    android:background="@color/viewcolor">
    <!--crap in the layout-->
</LinearLayout>

What did I miss?

+2  A: 

I remember that I worked around this error by using state drawable instead of state color. For some reason layout background just doesn't work with stateful colors. So try creating a stateful drawable (for example list of shape drawables with different colors) and use it as background.

res/drawable/pressed.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
   <solid android:color="#ff33ffff" />
 </shape>

res/drawable/normal.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
   <solid android:color="#ff000000" />
 </shape>

res/drawable/background.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <item android:state_pressed="true" android:drawable="@drawable/pressed" />
    <item android:drawable="@drawable/normal" />
</selector>

Then use background.xml drawable as background :)

Konstantin Burov
That worked perfectly. It's strange that the color doesn't work outright, though. Thanks!
iandisme
Instead of using shapes in your drawable the android:drawable attribute accepts a color resource (e.g. @color/black).
Austyn Mahoney
+2  A: 

Instead of using shapes in your drawable, you can use the android:drawable attribute which accepts a color resource (e.g. @color/black).

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/myview"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="top"
    android:background="@drawable/myDrawable">
    <!--crap in the layout-->
</LinearLayout>

myDrawable.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <item android:state_focused="true" android:drawable="@color/black" /> <!-- focused -->
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/black" /> <!-- focused and pressed-->
    <item android:state_pressed="true" android:drawable="@color/green" /> <!-- pressed -->
    <item android:drawable="@color/black" /> <!-- default -->
</selector>
Austyn Mahoney