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.
Related videos on Youtube
Author by
Mayec
Updated on March 07, 2022Comments
-
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 about 9 yearsThat 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 about 9 yearsOh sorry. If you use this snipped outside of onCreate, you need to use mActivity.getTheme(). (onCerate: mActivity = this)
-
Mayec about 9 yearsI 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 about 9 yearsIt 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 almost 9 years@Mayec If my answer helped you, it would be great if you could mark it as accepted answer :)
-
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 over 5 years@Mayec / for anyone facing this problem now - try using
typedValue.resourceId
, that worked for me. -
Alex Huiculescu almost 5 yearsFor anyone getting a different color, please check that the R class is your application's resources class, and not the android.R class
-
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.