How can I get the primary color from my app theme?

15,131

Solution 1

Use this:

TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
int color = typedValue.data;

Solution 2

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.

Share:
15,131

Related videos on Youtube

Mayec
Author by

Mayec

Updated on March 07, 2022

Comments

  • Mayec
    Mayec about 2 years

    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?

  • Mayec
    Mayec about 9 years
    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?
  • ByteHamster
    ByteHamster about 9 years
    Oh sorry. If you use this snipped outside of onCreate, you need to use mActivity.getTheme(). (onCerate: mActivity = this)
  • Mayec
    Mayec about 9 years
    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?
  • JavierSegoviaCordoba
    JavierSegoviaCordoba about 9 years
    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...
  • ByteHamster
    ByteHamster almost 9 years
    @Mayec If my answer helped you, it would be great if you could mark it as accepted answer :)
  • Mayec
    Mayec almost 9 years
    @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.
  • n_r
    n_r over 5 years
    @Mayec / for anyone facing this problem now - try using typedValue.resourceId, that worked for me.
  • Alex Huiculescu
    Alex Huiculescu almost 5 years
    For anyone getting a different color, please check that the R class is your application's resources class, and not the android.R class
  • gregn3
    gregn3 almost 4 years
    @Mayec @ AlexHuiculescu I had this issue too, check that you are setting the correct theme and overriding colorPrimary , colorPrimaryDark in your custom child theme.