I'm trying to use "?colorAccent" inside a text color selector element, but whenever the state in question is activated, the text shows red instead of my actual colorAccent value. I've isolated the problem to a minimum of files and posted the relevant snippets below. I've also uploaded the full project here: https://github.com/danh32/ColorAccentSelector if it's of any further help.
1) I have a ListView set to single choice 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 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, which should show solid black when 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's theme is this, which should set the ?colorAccent value to ##ff4081 (material pink A200):
<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>
Yet still when I run, I see the following:
If I change the CheckedTextView to have textColor="?colorAccent" directly instead of using a selector, the color is the correct pink value. Is there a way to get this working inside the selector?
Thanks to https://stackoverflow.com/a/23210511/462252 for the answer, apparently you just can't use an attr reference within a color selector. I ended up taking his advice and creating a new attr specific to my app called textColorSelector so that I can swap out my selector in the theme.
I'd be happy to share more specifics if anyone would find it useful.
EDIT: The code I used:
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="checkedTextSelector" format="reference"/>
</resources>
themes.xml (notice each child theme specifies a different a '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 (the other files just specify 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>
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