I would like to do a simple thing: Define a drawable which has exacly same background colour as system state-pressed background colour. I do it like this in res/drawables/my_drawable.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_selected="true"> <color android:color="?android:attr/colorPressedHighlight"/> </item> <item android:state_selected="false"> <color android:color="@color/section_list_background"/> </item> </selector>
I always get:
java.lang.UnsupportedOperationException: Cant convert to color: type=0x2
Any clues?
Regards
Use app:drawableTint="@color/yourColor" in the xml instade android:drawableTint="@color/yourColor" . It has the backward compatibility.
Note: A color resource can also be used as a drawable in XML. For example, when creating a state list drawable, you can reference a color resource for the android:drawable attribute ( android:drawable="@color/green" ).
You might need to do the following to fix your problem:
1) Define 2 colors for each theme in your colors file:
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="my_color_dark">#ff33B5E5</color> <color name="my_color_light">#ff355689</color> </resources>
2) Create file res/values/attrs.xml with contents:
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="my_color" format="reference" /> </resources>
3) Assuming you have 2 themes in your styles.xml (Theme.dark
and Theme.light
) define:
<style name="Theme.dark" parent="@style/Theme.Sherlock"> <item name="my_color">@color/my_color_dark</item> </style> <style name="Theme.light" parent="@style/Theme.Sherlock.Light"> <item name="my_color">@color/my_color_light</item> </style>
4) Use the color in a drawable:
<color android:color="?attr/my_color"/>
Hope this should fix your problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With