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

This should do the job:

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

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

android:theme="@style/Theme.BlueTheme"

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

setTheme(R.style.Theme_BlueTheme)

in onCreate().

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

Leave a Comment