Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change actionbar color programmatically more than once

Tags:

android

this is a quick fix that i found

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

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)))

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)


getColor is deprecated. use ContextCompat :

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

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.


If you want to avoid deprecation you can used

val mActionBar: ActionBar? = supportActionBar    
mActionBar.setBackgroundDrawable(ColorDrawable(ContextCompat.getColor(this, R.color.red)))

Kotlin Language