Is it confirmed that I cannot use themed color attribute, in color state list resource?
I have the following code which works pretty perfect.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#ffffffff" /> <!-- pressed -->
<item android:state_focused="true" android:color="#ffffffff" /> <!-- focused -->
<item android:state_selected="true" android:color="#ffffffff" /> <!-- selected -->
<item android:color="#ff000000" /> <!-- default -->
</selector>
<!-- Use duplicateParentState, so that we can receive click event from parent linear layout -->
<TextView
android:id="@+id/name"
android:gravity="center_vertical"
android:padding="5dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="48dp"
android:duplicateParentState="true"
android:textColor="@color/home_menu_text_view_selector"
android:drawablePadding="10dp" />
However, if I tune the code to the following, it no longer work. When in default mode, it no longer change to my desired color. ?android:attr/textColorPrimary
suppose be a black color (#ff000000) in my case.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#ffffffff" /> <!-- pressed -->
<item android:state_focused="true" android:color="#ffffffff" /> <!-- focused -->
<item android:state_selected="true" android:color="#ffffffff" /> <!-- selected -->
<item android:color="?android:attr/textColorPrimary" /> <!-- default -->
</selector>
Someone asked a similar question Can a selector resource use a color defined in a style? as mine, but no concrete answer yet. Hence, I throw out this ball again. :)
FYI, I believe this has been fixed in API 23. You should be able to declare attributes inside of color state lists now. – Alex Lockwood
Not because less expertise on English but because less expertise on core Android, I am lacking words to explain this, Still writing the answer to make a point which I have in my mind for this topic.
Android Attribute (attr)
and Android Color
both are completely separate things. Just because attr also defines color value you can't relate it with the single format color.
attr
is used to define many other things about a view, its reference,color,dimension,type etc. Even when you don't pass any format it takes some format's default value to use to apply the style attribute.
For example : You can use ImageView to show image by setting as src and also can take one LinearLayout and show image by setting it to background. If you are using Layout instead ImageView to show image then you can't accept it to do auto scale like center crop,fitXY that's only possible with ImageView.
Same way while writing selector
you can pass color and that's either a hexa color code or a defined color, But you can't set attr
as color
because selector item's android:color is accepting only color value while attr by default have some other formats also like type.
Single Line Answer : attr is not color, yes it has one format say color, but color is not the only format that's attr is containing so you can't use attr instead color.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With