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

2

29

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?

Olga answered 12/2, 2015 at 22:58 Comment(0)
G
59

Use this:

TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
int color = typedValue.data;
Grabble answered 12/2, 2015 at 23:1 Comment(10)
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?Olga
Oh sorry. If you use this snipped outside of onCreate, you need to use mActivity.getTheme(). (onCerate: mActivity = this)Grabble
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?Olga
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...Puberty
@Olga If my answer helped you, it would be great if you could mark it as accepted answer :)Grabble
@Grabble 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.Olga
@Olga / for anyone facing this problem now - try using typedValue.resourceId, that worked for me.Radon
For anyone getting a different color, please check that the R class is your application's resources class, and not the android.R classPanta
@Olga @ AlexHuiculescu I had this issue too, check that you are setting the correct theme and overriding colorPrimary , colorPrimaryDark in your custom child theme.Sharynshashlik
If anyone is interested in colorSecondary, then use com.google.android.material.R.attr.colorSecondaryEnschede
C
12

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.

Caravette answered 6/7, 2021 at 12:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.