27

In my Android java code, how can I reference the color "colorPrimary" set in my theme?

I have the following theme definition:

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">

    <item name="colorPrimary">@color/myColor1</item>
    <item name="colorPrimaryDark">@color/myColor2</item>      
    <item name="colorControlNormal">@color/myColor3</item>
    <item name="colorControlActivated">@color/myColor4</item>

</style>

I could reference the color resource directly (R.color.myColor1), but I would prefer to reference the theme's primaryColor setting, so that it stays consistent if the colorPrimary changes in the future.

Is this possible?

Ricardo A.
  • 685
  • 2
  • 8
  • 35
Mayec
  • 373
  • 1
  • 3
  • 9

2 Answers2

56

Use this:

TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
int color = typedValue.data;
ByteHamster
  • 4,884
  • 9
  • 38
  • 53
  • 2
    That seems to be getting a different color, which looks like a generic android blue. Could it be that getTheme() is getting the default theme, not the one that's actually defined for the app? – Mayec Feb 12 '15 at 23:11
  • 1
    Oh sorry. If you use this snipped outside of onCreate, you need to use mActivity.getTheme(). (onCerate: mActivity = this) – ByteHamster Feb 12 '15 at 23:18
  • I am calling it inside a method of the main Activity (let's call it "myListActivity"). So, the call happens in myListActivity.myMethod(). In this case, wouldn't it also be that this = myListActivity? – Mayec Feb 12 '15 at 23:48
  • 1
    It should work. I used it a lot of times inside fragment which is inside other fragment and zero problems. If you can, call directly with gettheme if not, MainActivity.getTheme... Or getactivity.getTheme... – JavierSegoviaCordoba Feb 13 '15 at 00:01
  • @Mayec If my answer helped you, it would be great if you could mark it as [accepted answer](http://meta.stackexchange.com/a/5235) :) – ByteHamster Jul 25 '15 at 16:27
  • @ByteHamster It's hard to be sure now, 5 months later, but I seem to remember that I never managed to make your solution work, which is why I didn't mark it as accepted answer. In any case, thank you ByteHamster, I appreciate your help. – Mayec Jul 26 '15 at 19:15
  • 2
    @Mayec / for anyone facing this problem now - try using `typedValue.resourceId`, that worked for me. – n_r Oct 03 '18 at 07:57
  • 1
    For anyone getting a different color, please check that the R class is your application's resources class, and not the android.R class – Alex Huiculescu Jul 19 '19 at 09:16
  • @Mayec @ AlexHuiculescu I had this issue too, check that you are setting the correct theme and overriding **colorPrimary** , **colorPrimaryDark** in your custom child theme. – gregn3 May 13 '20 at 20:02
8

You can get your current primary (or primaryVariant, etc) color by using ?attr/colorPrimary as the color in XML.

For example, I have a SVG and I need to set the background same as my current primary color. So I can do this like:

<path
        android:fillColor="?attr/colorPrimary"
        android:pathData="M0,0h800v800h-800z" />

Even if you change the theme, the background color will automatically change as well.

Mahdi
  • 81
  • 2
  • 5