Using appcompat? ColorAccent in color selector doesn't seem to work

I'm trying to use "? ColorAccent" inside a text color selector element, but whenever an activated state is fired, the text shows red instead of the actual colorAccent value. I've isolated the problem to a minimum of files and posted the relevant snippets below. I also downloaded the complete project here: https://github.com/danh32/ColorAccentSelector if it helps.

1) I have a ListView for single selection mode so that its rows can be checked.

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listView = (ListView) findViewById(R.id.listview);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listView.setAdapter(new ItemAdapter());
    }


}

      

2) Each row is just a CheckedTextView, so I can manipulate its textColor based on the checked state. row.xml:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textSize="16sp"
    android:padding="8sp"
    android:textColor="@color/row_text_color" />

      

3) @ color / row_text_color has the following value, which should appear solid black if unchecked, and my colorAccent value when checked:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="?colorAccent" />
    <item android:color="@android:color/black" />
</selector>

      

4) My app theme is what should set the colorAccent value to ## ff4081 (pink A200 stuff):

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->

        <item name="colorPrimary">#3f51b5</item>
        <item name="colorPrimaryDark">#303f9f</item>
        <item name="colorAccent">#ff4081</item>

    </style>

</resources>

      

However, when I run I see the following:

screenshot

If I changed the CheckedTextView so that instead of using textcolor = "? ColorAccent" instead of using a selector, the color was the correct pink value. Is there a way to make this work inside a selector?

+3


source to share


1 answer


Thanks to fooobar.com/questions/63190 / ... for the answer, apparently you just can't use the attr link in the color selector. I ended up taking his advice and creating a new attr specific to my application called textColorSelector so that I can change my selector in the theme.

I would be happy to share more specific features if anyone finds it helpful.

EDIT: The code I used:

attrs.xml

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

      



themes.xml (note that each child theme specifies a different "checkedTextSelector"):

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- specify enter and exit transitions -->
        <item name="android:windowContentTransitions" tools:targetApi="21">true</item>
        <item name="android:windowEnterTransition" tools:targetApi="21">@android:transition/explode</item>
        <item name="android:windowExitTransition" tools:targetApi="21">@android:transition/explode</item>
    </style>

    <style name="AppTheme.Blue">
        <item name="colorPrimary">@color/material_blue_500</item>
        <item name="colorPrimaryDark">@color/material_blue_700</item>
        <item name="colorAccent">@color/material_purple_a400</item>
        <item name="checkedTextSelector">@color/nav_row_text_color_blue</item>
    </style>

    <style name="AppTheme.Red">
        <item name="colorPrimary">@color/material_red_500</item>
        <item name="colorPrimaryDark">@color/material_red_700</item>
        <item name="colorAccent">@color/material_light_blue_a400</item>
        <item name="checkedTextSelector">@color/nav_row_text_color_red</item>
    </style>

    <style name="AppTheme.Pink">
        <item name="colorPrimary">@color/material_pink_500</item>
        <item name="colorPrimaryDark">@color/material_pink_700</item>
        <item name="colorAccent">@color/material_light_blue_a400</item>
        <item name="checkedTextSelector">@color/nav_row_text_color_pink</item>
    </style>

    <style name="AppTheme.Purple">
        <item name="colorPrimary">@color/material_purple_500</item>
        <item name="colorPrimaryDark">@color/material_purple_700</item>
        <item name="colorAccent">@color/material_blue_a400</item>
        <item name="checkedTextSelector">@color/nav_row_text_color_purple</item>
    </style>

    <style name="AppTheme.Green">
        <item name="colorPrimary">@color/material_green_500</item>
        <item name="colorPrimaryDark">@color/material_green_700</item>
        <item name="colorAccent">@color/material_purple_a400</item>
        <item name="checkedTextSelector">@color/nav_row_text_color_green</item>
    </style>
</resources>

      

nav_row_text_color_blue.xml (other files only indicate different colors):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/material_blue_500" />
    <item android:state_checked="true" android:color="@color/material_purple_a400"/>
    <item android:color="@color/text_black_primary"/>
</selector>

      

+4


source







All Articles