0

I have the following selector and it works pretty well.

<?xml version="1.0" encoding="utf-8"?>
<selector android:exitFadeDuration="@android:integer/config_mediumAnimTime"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/the_color" />
    <item android:drawable="@android:color/transparent" />
</selector>

Note, I can use Color for drawable, as in drawable-resouce.html

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").

Pretty good. Now, I want to try out theme.

home_menu_text_view_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector android:exitFadeDuration="@android:integer/config_mediumAnimTime"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="?attr/homeMenuTextViewPressedColor" />
    <item android:drawable="@android:color/transparent" />
</selector>

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="homeMenuTextViewPressedColor" format="color" />
</resources>

themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.X.Light.DarkActionBar" parent="Theme.Sherlock.Light.DarkActionBar">
        <item name="homeMenuTextViewPressedColor">@color/home_menu_text_view_pressed_color_inverse_holo_light</item>
    </style>
</resources>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="home_menu_text_view_pressed_color_inverse_holo_light">#ff4fc5f1</color>
</resources>

Now, I will getting the error :-

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

Any idea how I can resolve this?

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

1 Answers1

0

You are trying to convert an attribute to a color, the selector needs a color/drawable. If I understand correctly what you're trying to achieve, you can get rid of it completely.

home_menu_text_view_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector android:exitFadeDuration="@android:integer/config_mediumAnimTime"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/home_menu_text_view_pressed_color_inverse_holo_light" />
    <item android:drawable="@android:color/transparent" />
</selector>

colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="home_menu_text_view_pressed_color_inverse_holo_light">#ff4fc5f1</color>
</resources>

The thing with the theme is that you need to find out what exactly you are trying to style. For instance:

themes.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.X.Light.DarkActionBar" parent="Theme.Sherlock.Light.DarkActionBar">
        <item name="android:actionDropDownStyle">@style/MyDropDownNav</item>
    </style>
    <style name="MyDropDownNav">
        <item name="android:dropDownSelector">@drawable/home_menu_text_view_selector</item>
    </style>
</resources>

Hope this helps.

EDIT

Never mind that, I understand what you're trying to do now. A similar problem is addressed here:

Can a selector resource use a color defined in a style?

Community
  • 1
  • 1
Roger Rapid
  • 956
  • 1
  • 8
  • 28