I have several styles in the app.

<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="toolbarStyle">@style/Toolbar</item> <item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item> <item name="colorAccent">@color/accent</item> </style> <style name="GreenStyle"> <item name="colorPrimary">@color/primary_green</item> <item name="colorPrimaryDark">@color/primary_green_dark</item> </style> <style name="BrownStyle"> <item name="colorPrimary">@color/primary_brown</item> <item name="colorPrimaryDark">@color/primary_brown_dark</item> </style> <style name="TealStyle"> <item name="colorPrimary">@color/primary_teal</item> <item name="colorPrimaryDark">@color/primary_teal_dark</item> </style> <style name="AppTheme.Main.Green" parent="GreenStyle" /> <style name="AppTheme.Main.Brown" parent="BrownStyle" /> <style name="AppTheme.Main.Teal" parent="TealStyle" /> 

When activating, I set the theme selected in the settings, for example:

  setTheme(R.style.AppTheme_Main_Green); setContentView(R.layout.activity_weather); 

Here is an excerpt from colors.xml

 <color name="accent">#FFC107</color> <color name="primary_green">#4CAF50</color> <color name="primary_green_dark">#388E3C</color> <color name="primary_brown">#795548</color> <color name="primary_brown_dark">#5D4037</color> 

The colors of the elements on the activation are set correctly, everything is fine. However, if I want to manually repaint an element and refer to the primaryColor property, the system takes the primaryColor color not set in my style, but from the default colorPrimary. In my case it is blue.

I appeal like this:

 setFillColor(ContextCompat.getColor(context, R.color.colorPrimary)); 

or so:

 <gradient android:angle="90" android:startColor="@color/colorPrimary" android:endColor="@color/accent" /> 

The @ color / accent call works correctly, since it is defined in the colors.xml file

However, if you refer to the accent color through @ colors / colorAccent, then the accent color also resets to the default color, and the color from my style is not used.

How to work with colors through "colorAccent" and "colorPrimary" so that they cling from my style?

Thanks in advance for your help.

    1 answer 1

    In styles, you define attributes that contain links to color resources.

    In your code, you take the color from the color resources.

    You also need to get the color of the link stored in the attribute:

     <gradient android:angle="90" android:startColor="?colorPrimary" android:endColor="?colorAccent" /> 

    When doing so, keep in mind that in xml-drawable attributes cannot be used as colors on the API<21 . In this case, you will have to create an attribute-link and in each style specify a link to a separate xml-drawable file xml-drawable


    Programmatically, getting a color from an attribute is not a trivial task, but here is the working code:

     public static int getColor(Context ctx, int addressInRClass) { int colorId; int[] attrs = new int[]{addressInRClass}; TypedArray ta = ctx.obtainStyledAttributes(attrs); colorId = ta.getColor(0, Color.GRAY); ta.recycle(); return colorId; } 

    Call this:

     int colorPrimary = getColor(context, R.color.colorPrimary);