Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android selector.xml with custom attributes throwing XmlPullParserException

I have run into a problem using custom attributes. please help..

I have custom attributes defined in attr.xml

<attr format="reference" name="btnPressed" />
<attr format="reference" name="btnNormal" />

and I have got a theme

<style name="MyTheme" parent="@style/Theme.Sherlock.Light.NoActionBar">
   <item name="btnPressed">@drawable/fav_icon</item>
   <item name="btnNormal">@drawable/not_fav_icon</item>
</style>

In above fav_icon and not_fav_icon are my images.

I am having a selector favorite_btn.xml defined for toggle button.

<selector xmlns:android="http://schemas.android.com/apk/res/android">   
    <item android:state_checked="true" android:drawable="?attr/btnPressed"/> 
    <item android:state_checked="false" android:drawable="?attr/btnNormal"/> 
    <item android:drawable="?attr/btnNormal"/>
</selector>

Now I am setting the selector to my toggle button.

        <ToggleButton
            android:id="@+id/station_fav_star"
            android:layout_width="42dip"
            android:layout_height="42dip"
            android:background="@drawable/favorite_btn"
            android:textOff=""
            android:textOn="" />

And I am applying the theme to my activity. But I am getting Xml Parse Exception as mentioned below.

Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #11: tag requires a 'drawable' attribute or child tag defining a drawable

I am doing the same method of using custom attributes for setting the images to normal buttons and it is working. But when custom attributes are defined in selectors, it is not working.

Please let me know if I am missing anything.

I have already checked this link Android color selector doesn't work with custom attributes

In that it states for color changes. In my case they are drawables. If there is any other way around, please suggest the solution.

Thanks in advance..

like image 807
vijaykumarg Avatar asked Dec 15 '12 12:12

vijaykumarg


1 Answers

Android did not support theme attributes in drawable XML until L preview. Prior to L preview, any theme attributes will be treated as invalid values and throw an inflation exception or resolve to 0 / null depending on the type.

like image 116
alanv Avatar answered Nov 04 '22 17:11

alanv