Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color reference in <item>-Tag, inside <layer-list>

I have a question about using references in a layer-list drawable.

I want to use a custom button in my app, made of a layer-list.

  • That is the final drawable btn.xml for the button, made of a selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_normal"/>
</selector>
  • The pressed-state-drawable btn_pressed looks like that:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="?custom_color" />
        </shape>
    </item>
    <item android:drawable="@drawable/btn_normal"/>
</layer-list>

The essential part is the line, where i declare the color to be a reference to custom_color, that i define as follows.

  • attrs.xml:
<resources>
    <attr name="custom_color" format="reference|color" />
</resources>
  • themes.xml:
<resources>
    <style name="MyTheme" parent="android:Theme.Light.NoTitleBar">
        <item name="custom_color">#ff33b5e5</item>
    </style>
</resources>

Eclipse gives me no errors at all, and it compiles just fine. But when i start the application on my ICS Nexus S, or the Emulator (no matter what version), it crashes. Logcat points the following out:

03-23 14:33:38.832: E/AndroidRuntime(636): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/btn.xml from drawable resource ID #0x7f020006

[...]

03-23 14:33:38.832: E/AndroidRuntime(636): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/btn_pressed.xml from drawable resource ID #0x7f020009

If I comment out, the shape-element in the layer-list, or set the color hard-coded, everything works. So there seems to be a problem referencing attributes in a layer-list.

So, does anyone know a solution to this problem? I want to change colors in that layer-list, depending on the theme, that my application is using.

like image 952
msal Avatar asked Nov 14 '22 08:11

msal


1 Answers

Ok, seems like this is a bug that wasn't looked at until Android L.
More details on the Android Issue Tracker: Issue 26251

It should be fixed in Android L, but at least testing with the Android L Preview in the Emulator it doesn't seem to work completely, yet. At least it won't crash the app, but instead of showing the correct color, it just gives me transparency (#00ffffff). Maybe this isn't the case on the devices and/or once Android L stable is released.

like image 154
msal Avatar answered Nov 16 '22 03:11

msal