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

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 🙂

Leave a Comment