Change actionbar color programmatically more than once

Solution 1:

this is a quick fix that i found

mActionBar.setBackgroundDrawable(new ColorDrawable(0xff00DDED));
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayShowTitleEnabled(true);

Solution 2:

Try this,

Method1:

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(0xff00FFED));

Method2:

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources()
                    .getColor(R.color.bg_color)));

Method3:

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#3A1212")));

Kotlin

supportActionBar?.setBackgroundDrawable(ColorDrawable(ContextCompat.getColor(this, android.R.color.black)))

Solution 3:

I had the same problem, answer from user1634451 worked but only once (would not enable several color switches in a row)

This definitely fixed it:

bar.setBackgroundDrawable(new ColorDrawable(getResources()
                    .getColor(R.color.app_bar_online)));

Instead of directly linking to the color doing new ColorDrawable(R.color.app_bar_online)

Solution 4:

getColor is deprecated. use ContextCompat :

bar.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(context, R.color.app_bar_online)));

Solution 5:

If you want to set the color of the ActionBar and have the color as a String, this seems to work for me.

    getSupportActionBar().setBackgroundDrawable(
            new ColorDrawable(Color.parseColor("#993b3c4e")));

You may have to enable & disable the title to get it to refresh/display properly like in the answer given by user1634451, but I didn't need to in my case.