I am using attr to create a selector drawable for my project so that once i change theme colors, i dont have to make any change in the drawable file. I am using following libs:
compile 'com.android.support:appcompat-v7:+'
compile 'com.android.support:cardview-v7:+'
compile 'com.android.support:design:22.2.0'
Here is the source code for drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="?attr/colorPrimary" android:state_enabled="true" android:state_window_focused="false"/>
<item android:drawable="?attr/colorPrimaryDark" android:state_pressed="true"/>
<item android:drawable="@android:color/darker_gray" android:state_enabled="false"/>
<item android:drawable="?attr/colorPrimary"/>
</selector>
in this same code, if i replace attributes with colors defined in colors.xml file, the same drawable works.
Sample drawable with colors:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/color_primary" android:state_enabled="true" android:state_window_focused="false"/>
<item android:drawable="@color/color_primary_dark" android:state_pressed="true"/>
<item android:drawable="@android:color/darker_gray" android:state_enabled="false"/>
<item android:drawable="@color/color_primary"/>
</selector>
Thanks in advance!
Finally, found the problem. There is a bug in android [pre-lollipop] OS which doesnt allow you to use attr in drawable. Here is the link to bug:
Android dev team has released a fix but it works on android L and above.For workaround to this problem, refer to following solution: