Change Android toolbar color

I have an application that uses android.support.v7.widget.Toolbar

. Each section of the application is one Fragment

that is accessed throughsupport.v4.widget.DrawerLayout

I need to change the color Toolbar

depending on which section is shown (especially for the client).

I have defined some colors in colors.xml

so that I can do 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 that every time I use primaryColor

(the original foreground color from the toolbar) is now rendered with the new toolbar color.

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

I suspect that changing the "Toolbar" background changes the definition itself primaryColor

(which doesn't make sense to me). As I have no other idea of ​​how unbound elements in unbound activities start to use the same color.

This is mistake? Anyone with this problem? Are there any workarounds available?

Thank you for your help.

+3


source to share


1 answer


First of all, themes are immutable , so it is not possible to change the main color of the application.

And try using getSupportActionBar().setBackgroundDrawable()

.



I am guessing it is something else that is causing the problem. Can you post more code?

+1


source







All Articles