Maraumax
Maraumax

Reputation: 704

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 ?

Upvotes: 4

Views: 6209

Answers (2)

gelakinetic
gelakinetic

Reputation: 523

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

Upvotes: 15

Praveen
Praveen

Reputation: 331

First:
Problem is with your color en-coding.
Color is specified as a combination of RGB (Red, Green, Blue) where as in photoshop 0xff00 means its a 16bit/15bit color whose first byte has 0xFF value which would have red component but not red exactly..

Now for android many other things are specified in it. a color is encoded as ARGB whre it is Alpha, Red Green Blue so the color value for opaque red would be 0xFFFF0000. Color in Android is 32bit Value.

Second:
android:textColor="?attr/tabsTextColor" accepts a color value. Passing selector is wrong.

Third
Every item in seletor should have a reference to a drawable. Where is that? You should have a drawable in your selecter as in somethingselector.xml.
<item android:state_focused="false"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/something" />

So in your code you can use like this android:drawableRight="@drawable/somethingseletor"

Upvotes: -4

Related Questions