34

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">
    <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?

iandisme
  • 6,346
  • 6
  • 44
  • 63
  • if somebody wants to get full solution, check this repository: https://github.com/shamanland/AndroidLayoutSelector there is custom clickable/checkable ```LinearLayout``` like a ```ToggleButton``` – Oleksii K. Oct 17 '13 at 12:21

2 Answers2

51

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">
    <item android:state_pressed="true" android:drawable="@drawable/pressed" />
    <item android:drawable="@drawable/normal" />
</selector>

Then use background.xml drawable as background :)

Artem Russakovskii
  • 21,516
  • 18
  • 92
  • 115
Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93
49

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">
    <!-- other views in layout-->
</LinearLayout>

my_drawable.xml

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

In my_drawable.xml you need to make sure that the colors you specify are defined in res/values/colors.xml, or this won't work.

If you want to use an image instead of a color change from a color resource to a drawable resource. Example:

android:drawable="@color/YOUR_COLOR_HERE"
android:drawable="@drawable/YOUR_IMAGE_HERE"
Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85
  • 2
    This didn't quite work for me out of the box. I had to change android:drawable in the selector items to android:color and then it worked fine. – emmby Mar 17 '11 at 19:18
  • @emmby: I added an explanation on what you need to do to use this code "out of the box". – Austyn Mahoney Jul 27 '11 at 17:02
  • 3
    Works perfect. For those who aren't familiar with a colors.xml file as I wasn't, see http://stackoverflow.com/questions/3769762/android-color-xml-resource-file – dajaffe Jan 05 '13 at 16:51
  • It does work, 34 other people have confirmed that it does. I also just did a test and it still works. Check your malformed XML. – Austyn Mahoney Jun 26 '13 at 23:47
  • This does work, however I think it is worth pointing out that while 'android:drawable' will accept colors of the form '@color/...' it will NOT accept colors like '#ffffff'. – Dave Sep 13 '13 at 12:41
  • Yes, I explain that you need a "Color Resource" in the answer. Hopefully people understand that a color resource is different from a color string literal. – Austyn Mahoney Sep 17 '13 at 17:40
  • 1
    This works and should be the accepted answer. Take a look here as well http://goo.gl/QGRoCt – Sotti Nov 29 '14 at 00:17