I am using attr to create a selector drawable for my project so that once i change theme colors, i dont have to make any change in the drawable file. I am using following li
I dunno if it's still relevant to you, but I was struggling with the same thing and I think I have found a workaround (kinda). The direct use of ?attr/ is not working (working on api 21 and up, but even then it's not working with color selectors (only with drawables).
So I did it like this (giving you my own example). Create an atribute in attr.xml
Then in all the themes you're using, add the attribute like so (f.e. for the Light Theme):
- @color/text_color_selector_light
or for the dark theme(default):
- @color/text_color_selector
Now my text_color_selector.xml (both) look like this:
and when I want to use them, f.e in tinting my custom image view, I use:
You can also reitieve it programmatically by using TypedValue, like so:
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
theme.resolveAttribute(R.attr.nav_item_color_selector, typedValue, true);
XmlResourceParser parser = viewGroup.getContext().getResources().getXml(typedValue.resourceId);
try {
ColorStateList sl = ColorStateList.createFromXml(viewGroup.getContext().getResources(), parser);
viewHolder.textView.setTextColor(sl);
} catch (Exception e) { }
I hope this helps :-)