Seraphim's Seraphim's - 1 year ago 61
Android Question

Get color value programmatically when it's a reference (theme)

Consider this:


<style name="BlueTheme" parent="@android:style/Theme.Black.NoTitleBar">
<item name="theme_color">@color/theme_color_blue</item>


<attr name="theme_color" format="reference" />


<color name="theme_color_blue">#ff0071d3</color>

So the theme color is referenced by the theme. How can I get the theme_color (reference) programmatically? Normally I would use
but not in this case because it's referenced!

Answer Source

This should do the job:

TypedValue typedValue = new TypedValue();
Theme theme = context.getTheme();
theme.resolveAttribute(R.attr.theme_color, typedValue, true);
int color =;

Also make sure to apply the theme to your Activity before calling this code. Either use:


in your manifest or call (before you call setContentView(int)):

setTheme( in onCreate()

I've tested it with your values and it worked perfectly.