18

Is there a way to change programmatically the primary colors. I would like to do it in code depending on the screen/state of the app.

Currently I can only set the colors in the theme (static) :

<item name="android:colorPrimary">@color/primary_color</item>
<item name="android:colorPrimaryDark">@color/dark_color</item>
<item name="android:colorBackground">@android:color/white</item>
<item name="android:colorAccent">@color/primary_color</item>
<item name="android:colorControlHighlight">@color/primary_color</item>
Edric
  • 24,639
  • 13
  • 81
  • 91
ebtokyo
  • 2,369
  • 4
  • 23
  • 33

4 Answers4

14

You can, of course, implement custom subclasses of View that have methods for setting colors.

You can also define multiple themes with you various color schemes.

Views look up theme information from the context when they are created. So to change the styles applied from a theme you will have to recreate your view hierarchy with a context that uses the right theme.

One way to do that, is to create a new ContextThemeWrapper and then get a LayoutInflator that uses that theme wrapper, remove the old version of your layout and re-inflate your layout.

Roughly:

ContextThemeWrapper themeWrapper = new ContextThemeWrapper(this, R.style.AppThemeWithColorScheme2);
LayoutInflater layoutInflater = LayoutInflater.from(themeWrapper);
viewContainer.removeAllViews();
layoutInflater.inflate(R.layout.my_layout, viewContainer, true );

If you are using Action Bar, that may be a bit more tricky, because the Action Bar is created once per activity.

Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81
yogurtearl
  • 3,035
  • 18
  • 24
  • 1
    Thanks, recreating the activity is not an issue in my use case.So I ended writing all the theme in the style.xml, the only difference between them is the colors. I will see if I can improve with a ContextThemeWrapper. – ebtokyo Aug 20 '14 at 16:55
1

USe this code for setting toolbarcolor and status bar (darker toolbar color)

toolbar.setBackgroundColor(toolbarColor);
factor=0.8f; 
int a = Color.alpha(toolbarcolor);
int r = Math.round(Color.red(toolbarcolor) * factor);
int g = Math.round(Color.green(toolbarcolor) * factor);
int b = Math.round(Color.blue(toolbarcolor) * factor);
int statusColor=Color.argb(a,
        Math.min(r, 255),
        Math.min(g, 255),
        Math.min(b, 255));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = MainActivity.this.getWindow();
    window.setStatusBarColor(statusColor);
}
  • Welcome to stack overflow. While this is indeed a programmatic way to change *some* colors, it is not a general way to change the app's primary color *everywhere*; that is, dynamically change the theme's color everywhere it is used. – ToolmakerSteve Nov 29 '19 at 21:57
-1

This is most practical with no error, there is no need to do any extra coding, just go to android tree project

res > values > colors then edit these codes:

 <resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
</resources>

add these under style:

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

add into manifest just after ".MainActivity" :

android:theme="@style/AppTheme.NoActionBar">
  • 5
    Welcome to stack overflow. The question asks how to change these colors *programmatically*. That means, change them at run-time, in *code*. – ToolmakerSteve Nov 29 '19 at 21:55
-1

This change everything in text

<item name="android:textColorPrimary">@color/textGrey</item>
    <item name="android:textColorSecondary">@color/textGrey</item>
    <item name="android:textColorTertiary">@color/textGrey</item>
    <item name="android:listDivider">@color/textGrey</item>
  • 5
    Welcome to stack overflow. The question asks how to change these colors *programmatically*. That means, change them at run-time, in *code*. – ToolmakerSteve Nov 29 '19 at 21:55