Eloi Navarro
Eloi Navarro

Reputation: 1445

Android Toolbar color change

I have an app which uses android.support.v7.widget.Toolbar. Every section of the app is a Fragment accessed through a support.v4.widget.DrawerLayout

I need to change the Toolbar color depending on which section is shown (client particular needs).

I defined some colors in the colors.xml so I can make something like:

changeToolbarColor(R.color.section_one);

/**/

private void changeToolbarColor(int color_res_id){
    Integer colorTo = getResources().getColor(color_res_id);
    toolbar.setBackgroundColor(colorTo);
}

The problem is, once I do this, every view using the primaryColor (the original primary color from the toolbar) now shows up using the new color of the Toolbar.

So if my Toolbar was green and I change it to red, now everything using the old green uses red instead.

I suspect, that the change of the Toolbars background changes the primaryColor definition itself (which makes no sense to me). Because I have no other idea of how unrelated elements in unrelated activities start using the same color.

Is this a bug? Anyone with this problem? Any workarounds available?

Thanks for your help.

Upvotes: 3

Views: 923

Answers (1)

Akshay Chordiya
Akshay Chordiya

Reputation: 4841

First of all themes are immutable, so it's not possible to change the primary color of the app.

And try using getSupportActionBar().setBackgroundDrawable().

I guess it's something else which is causing the issue. Can you post more code ?

Upvotes: 1

Related Questions