Reputation: 160
I'm trying to use a color defined in a stlyle in a selector but it is causing a Resources$NotFoundException.
First I added a new attribute to attr.xml:
<resources>
<attr name="unread_background" format="color" />
</resources>
Then I defined that attr value in styles.xml:
<style name="ThemeNoTitleBar" parent="android:Theme.NoTitleBar">
<item name="unread_background">#000000</item>
</style>
Then I tried to use that attr in my selector definition:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- other states snipped -->
<item android:state_selected="false"
android:drawable="?unread_background" />
</selector>
Lastly, the activity uses the ThemeNoTitleBar style theme in the manifest.
I've also tried creating a color in colors.xml and having it use the new attr but that also fails.
I'm obviously missing something but am not sure what to do to fix it. My intent is to create multiple themes and have the selector use the color in the currently selected theme.
Upvotes: 16
Views: 5257
Reputation: 1006
Here is something, that works by me.
attrs.xml:
<attr name="color_selection" format="reference"/>
styles.xml, as child of main theme:
<item name="color_selection">@color/selection_background_inverse</item>
shape_background_selected.xml in drawable folder:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?attr/color_selection"/>
</shape>
your selector file, in my case: selector_background_recyclerview:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shape_background_selected" android:state_activated="true" />
<item android:drawable="@drawable/shape_background_selected" android:state_pressed="true" /> <!-- pressed -->
<item android:drawable="@color/transparent" /> <!-- default -->
</selector>
finally, in your view's xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/selector_recyclerview_item_background"../>
Upvotes: 1
Reputation: 3134
<item android:state_selected="false"
android:drawable="?unread_background" />
this above section is wrong.
the drawable only take a reference to a drawable resource. Please see this link. http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Upvotes: 1
Reputation: 69228
Android button with different background colors Take a look onto the example. It looks like you need that.
Upvotes: -1