Change actionbar color programmatically more than once

Android

Android Problem Overview


I am using

getSherlockActivity().getSupportActionBar().setBackgroundDrawable(new ColorDrawable(0xff00ACED));

To change the color of my action bar in a fragment and it works. But if i open this fragment then open another fragment that calls this method with a different color the actionbar doesn't change to the desired color. Instead it turns to a white color instead of the color I set it to.

Android Solutions


Solution 1 - Android

this is a quick fix that i found

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

Solution 2 - Android

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

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

getColor is deprecated. use ContextCompat :

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

Solution 5 - Android

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.

Solution 6 - Android

If you want to avoid deprecation you can used

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

Kotlin Language

Solution 7 - Android

If you want want to change actionbar color or background pragmatically then simply use,

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getSupportActionBar().setBackgroundDrawable(getDrawable(R.drawable.white_background));
            }

white_background.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#fff" />

Solution 8 - Android

((AppCompatActivity) getActivity()).getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorPrimary)));

In the fragment and Java

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1634451View Question on Stackoverflow
Solution 1 - Androiduser1634451View Answer on Stackoverflow
Solution 2 - AndroidSilambarasanView Answer on Stackoverflow
Solution 3 - AndroidantoinemView Answer on Stackoverflow
Solution 4 - AndroidyusufonderdView Answer on Stackoverflow
Solution 5 - Androidwelshk91View Answer on Stackoverflow
Solution 6 - AndroidGDNarvaezView Answer on Stackoverflow
Solution 7 - AndroidNazmus SaadatView Answer on Stackoverflow
Solution 8 - AndroidStudy ChiragView Answer on Stackoverflow