Android LinearLayout with color resource: What am I doing wrong?
Asked Answered
P

2

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?

Plump answered 17/8, 2010 at 19:45 Comment(1)
if somebody wants to get full solution, check this repository: github.com/shamanland/AndroidLayoutSelector there is custom clickable/checkable LinearLayout like a ToggleButtonSparks
S
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 :)

Shutt answered 17/8, 2010 at 20:7 Comment(2)
That worked perfectly. It's strange that the color doesn't work outright, though. Thanks!Plump
Instead of using shapes in your drawable the android:drawable attribute accepts a color resource (e.g. @color/black).Transudate
T
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"
Transudate answered 17/8, 2010 at 21:50 Comment(7)
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.Invert
@emmby: I added an explanation on what you need to do to use this code "out of the box".Transudate
Works perfect. For those who aren't familiar with a colors.xml file as I wasn't, see #3770262Brennan
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.Transudate
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'.Molli
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.Transudate
This works and should be the accepted answer. Take a look here as well goo.gl/QGRoCtAble

© 2022 - 2024 — McMap. All rights reserved.