16

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.

toddler
  • 160
  • 1
  • 6

3 Answers3

1

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"../>
Fattum
  • 996
  • 1
  • 9
  • 23
1
<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

CChi
  • 3,054
  • 1
  • 20
  • 15
  • However, according to the documentation (http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html#attr_android:src), the src attribute in a BitmapDrawable can be theme attribute. – Joe Bowbeer Jul 31 '12 at 07:24
  • 1
    Still, even if the attribute was a reference and the style was a drawable you would still crash the system because Android only allows custom attributes(?whatever) to be used in Views (at least that is what I have deduced - I really want to be wrong though!) – HGPB Aug 24 '12 at 21:00
-1

Android button with different background colors Take a look onto the example. It looks like you need that.

Community
  • 1
  • 1
Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93