Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 323
Mayec Avatar asked Feb 12 '15 22:02

Mayec


People also ask

How do you find the primary color in a theme?

You can use class="mat-primary" and class="mat-accent" on HTML elements to get the primary and accent colours of your theme.

How do I change the color of my app theme?

In Android Studio, open themes. xml (app > res > values > themes > themes.

How do you get primary colors in flutter?

Clicking on ctrl + primarySwatch, You will be Headed to theme_data page and there you can change your theme color according to your convience.


1 Answers

Use this:

TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
int color = typedValue.data;
like image 155
ByteHamster Avatar answered Oct 06 '22 18:10

ByteHamster