Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it confirmed that I cannot use themed color attribute in color state list resource

Tags:

android

Is it confirmed that I cannot use themed color attribute, in color state list resource?

I have the following code which works pretty perfect.

color/home_menu_text_view_selector.xml

<?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>

layout/home_menu_row.xml

<!-- 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. :)

like image 646
Cheok Yan Cheng Avatar asked Mar 28 '13 02:03

Cheok Yan Cheng


1 Answers

Edited as per Comment received

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

Old Answer

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.

like image 87
MKJParekh Avatar answered Nov 09 '22 21:11

MKJParekh