Cannot use standard android color attribute in custom color selector

Posted by Manish Gupta on Stack Overflow See other posts from Stack Overflow or by Manish Gupta
Published on 2012-06-27T15:00:29Z Indexed on 2012/06/27 15:16 UTC
Read the original article Hit count: 608

Filed under:
|

So, android defines the following in themes.xml:

<style name="Theme">
    ...
    <item name="colorPressedHighlight">@color/legacy_pressed_highlight</item>
</style>

and:

<style name="Theme.Holo">
    ...
    <item name="colorPressedHighlight">@color/holo_blue_light</item>
</style>

I want to use this colorPressedHighlight as the background color for my custom Button when it is pressed. So I defined the following in res/color/app_button_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="?android:colorPressedHighlight"/>
    <item android:drawable="@android:color/transparent" />
</selector>

Finally, I define my custom ImageButton style:

<style name="App_ImageButtonStyle" parent="@android:style/Widget.ImageButton">
    <item name="android:gravity">center</item>
    <item name="android:background">@color/app_button_background</item>
</style>

I crash on app launch with the following call stack:

06-27 20:24:41.954: E/AndroidRuntime(532): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #6: <item> tag requires a 'drawable' attribute or child tag defining a drawable
06-27 20:24:41.954: E/AndroidRuntime(532):  at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:178)
06-27 20:24:41.954: E/AndroidRuntime(532):  at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:867)
06-27 20:24:41.954: E/AndroidRuntime(532):  at android.graphics.drawable.Drawable.createFromXml(Drawable.java:804)
06-27 20:24:41.954: E/AndroidRuntime(532):  at android.content.res.Resources.loadDrawable(Resources.java:1920)

I know that directly accessing @color/legacy_pressed_highlight or @color/holo_blue_light instead of accessing them through the colorPressedHighlight fixes the crash but it does not solve the problem. Themes can vary, hence I need to access it through the colorPressedHighlight attribute.

PS: I had a similar problem to which I haven't found an answer yet. Can someone please help!

© Stack Overflow or respective owner

Related posts about android

Related posts about android-theme