user1634451
user1634451

Reputation: 5212

Change actionbar color programmatically more than once

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.

Upvotes: 58

Views: 68905

Answers (8)

Silambarasan
Silambarasan

Reputation: 787

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

Upvotes: 47

Study Chirag
Study Chirag

Reputation: 31

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

In the fragment and Java

Upvotes: -1

lgn
lgn

Reputation: 187

If you want to avoid deprecation you can used

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

Kotlin Language

Upvotes: 4

Nazmus Saadat
Nazmus Saadat

Reputation: 973

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" />

Upvotes: -1

welshk91
welshk91

Reputation: 1663

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.

Upvotes: 5

yusufonderd
yusufonderd

Reputation: 3247

getColor is deprecated. use ContextCompat :

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

Upvotes: 11

antoinem
antoinem

Reputation: 503

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)

Upvotes: 15

user1634451
user1634451

Reputation: 5212

this is a quick fix that i found

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

Upvotes: 88

Related Questions