Search code examples
androidcolorsandroid-actionbarscale

Actionbar background color changes on scale


I used this code to change actionbar color that is used with appcompat lib:

actionBar.setBackgroundDrawable(new ColorDrawable(R.color.teleblue));

but in use color changes and in different screen sizes color is different. I don't want to clean the original question with tricky ways. how can I fix it?

thank you


Solution

  • R.color.teleblue is a resource identifier not a #AARRGGBB color.

    Depending on what type actionBar is some of following options may or may not be available:

    actionBar.setBackgroundResource(R.color.teleblue); // if it's Toolbar
    
    actionBar.setBackgroundDrawable( // if it's ActionBar
        new ColorDrawable(
            actionBar.getThemedContext().getResources().getColor(R.color.teleblue));
    
    actionBar.setBackgroundDrawable( // if it's ActionBar
        ContextCompat.getDrawable(
            actionBar.getThemedContext(),
            R.color.teleblue));
    
    actionBar.setBackgroundDrawable( // if it's Toolbar
        ContextCompat.getDrawable(
            actionBar.getContext(),
            R.color.teleblue));