Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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.

like image 985
toddler Avatar asked Sep 21 '10 06:09

toddler


People also ask

What is color state list Android?

A ColorStateList is an object you can define in XML that you can apply as a color, but will actually change colors, depending on the state of the View object to which it is applied.

How can we use custom colors in application?

Click New —> Values resource file menu item to popup the New Resource File dialog window. In the New Resource File dialog window, input keyword colors in the File name input box, select main in the Source set drop-down list, input values in the Directory name input text box, then click the OK button. Then colors.

How do I get color resources in Android?

Use ResourcesCompat. getColor(App. getRes(), R. color.

What is a selector Android?

A multiplexor of SelectableChannel objects. A selector may be created by invoking the open method of this class, which will use the system's default selector provider to create a new selector. A selector may also be created by invoking the openSelector method of a custom selector provider.


1 Answers

<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

like image 70
CChi Avatar answered Sep 20 '22 05:09

CChi