57

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.

Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
user1634451
  • 5,133
  • 6
  • 30
  • 43

8 Answers8

88

this is a quick fix that i found

mActionBar.setBackgroundDrawable(new ColorDrawable(0xff00DDED));
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayShowTitleEnabled(true);
user1634451
  • 5,133
  • 6
  • 30
  • 43
  • This only happened for me on one device... The blasted Motorola Xoom. However, this fixed the problem! In my case, toggling the display of the title needed to be reversed since I was not showing the title to begin with. – Justin Aug 06 '13 at 15:47
  • I can confirm this also fixed the issue on an LG-P880g. You my friend, are amazing! Thank you! – JVillella Oct 19 '14 at 15:20
  • I had to use a color resource when using the support action bar. getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.swift))); – Tyler Pfaff Sep 25 '15 at 23:34
  • Now ActionBars are long gone. Everybody uses Toolbars now – Pranav Mahajan Aug 25 '17 at 08:02
  • getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#363C54"))); – Ashwin H May 09 '18 at 12:44
46

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)))
Silambarasan
  • 767
  • 5
  • 19
15

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)

Community
  • 1
  • 1
antoinem
  • 503
  • 4
  • 12
  • 1
    I'm using `ContextCompat` instead of `getResources`, though, because I'm using the support library. Also, `getResources().getColor(int)` is deprecated now. – Ishita Sinha Jun 21 '16 at 09:49
11

getColor is deprecated. use ContextCompat :

bar.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(context, R.color.app_bar_online)));
yusufonderd
  • 3,237
  • 4
  • 21
  • 34
  • Thanks. In case if anyone picking color from color-array then use **bodyColor[index]** instead **ContextCompat.getColor(context, R.color.app_bar_online)**. – CoDe Aug 20 '16 at 04:58
  • Using this solution, I get a NullPointerException at this point (even a warning from AS). Why? – MUmla Aug 21 '17 at 12:18
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.

Community
  • 1
  • 1
welshk91
  • 1,613
  • 18
  • 16
4

If you want to avoid deprecation you can used

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

Kotlin Language

lgn
  • 119
  • 4
-1

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

Nazmus Saadat
  • 973
  • 9
  • 22
-1

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

In the fragment and Java