Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use theme with style, selector and attr on Android

I try to change text color with theme in a selector but i have already the same color : #fff (i havent this color in my colors.xml !)

Here is my selector.xml (in drawable/) :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@android:color/white" />
    <item android:state_focused="true" android:color="@android:color/white" />
    <item android:state_pressed="true" android:color="@android:color/white" />
    <item android:color="?attr/tabsTextColor" />
</selector>

My attrs.xml file :

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

Colors.xml file :

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- ... -->
    <color name="tabs_text_color">#ff0</color>
</resources>

And my theme.xml :

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.Custom" parent="@style/Theme.GreenDroid.NoTitleBar">
        <item name="tabsTextColor">@color/tabs_text_color</item>
    </style>
</resources>

I dont understant because text color is in red (get #f00 with photoshop) but not #ff00 !

Where is the mistake? Thanks

EDIT : I replace in my layout

android:textColor="@drawable/selector.xml"

By

android:textColor="?attr/tabsTextColor"

And the color is good ! I can't use selector with theme attr ?

like image 782
Maraumax Avatar asked Oct 19 '25 10:10

Maraumax


1 Answers

You cannot reference theme attrs within in a selector, but what you can do is create multiple selectors which each only reference color/drawable resources, and then use a reference attr to control which selector is used in your theme

like image 139
gelakinetic Avatar answered Oct 22 '25 02:10

gelakinetic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!