302

Details:

I'm extending ActionBarActivity.
Eclipse and SDK fully patched as of 2011-11-06.

<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14" />  

Deployed to Samsung device with Android 2.3.3
Application has android:theme="@android:style/Theme.Light"

Issue: application is light, but ActionBar is blue with grey icons, hardly visible against the blue background color. I also want the ActionBar to be light, so they grey icons are more visible.

I've tried modifying the styles but to no avail.
I'm probably missing something trivial.

How do I change the background color of the ActionBar of an ActionBarActivity using XML ?

Jonik
  • 80,077
  • 70
  • 264
  • 372
user77115
  • 5,517
  • 6
  • 37
  • 40
  • 2
    Closing this old chestnut. Points to Sannidhi, by popular demand, however the replies to this question span quite a few versions of Android, and in my context David Millers suggestion to use SherlockActionBar was most relevant. – user77115 Jun 24 '13 at 04:55
  • 1
    Little tidbit for others: If you make the first element in your XML view the same background as your ActionBar (in our case, white), the ActionBar turns grey. – Joshua Pinter Dec 31 '19 at 20:29
  • When you think about, TitleBar and ActionBar dont offer anything special. They are more like a nuisance, especially when you dont know them. A.better alternative would be to hide them and design your own title bar. You can add your own widgets with Views and drawables. As Title bar provides no widgets. And action bar can only have one drop down list, for all menuItems.Even alot of Apps have ditched Action Bar in favour of their own. –  Feb 07 '20 at 05:14

20 Answers20

539

As per documentation - "You can control the behaviors and visibility of the action bar with the ActionBar APIs, which were added in Android 3.0 (API level 11)."

So, ActionBar will not work for your target environment which is at API level 10 (Android 2.3.3).

Just in case, if you target for minimum API level 11 , you can change ActionBar's background color by defining custom style, as:

<resources>
    <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">ANY_HEX_COLOR_CODE</item>
    </style>
</resources>

And, set "MyTheme" as theme for application / activity.

Papershine
  • 4,995
  • 2
  • 24
  • 48
lupchiazoem
  • 8,026
  • 6
  • 36
  • 42
  • 12
    @AlexanderFragotsis use `@color/my_color`. – lethargicpanda Jan 07 '13 at 19:54
  • 31
    The same approach should be used if you are using ActionBarCompat library. The only difference is that you should change **actionBarStyle** (without *android:* prefix) item to define how actionbar will look on pre-3.0 Android devices – Alex Semeniuk Nov 14 '13 at 07:55
  • 3
    Is there any possibility to, instead of putting the color's HEX code, reference a `@android:color` or even a custom color defined in `colors.xml`? I've tried it with no success. – jmrodrigg Jul 30 '14 at 22:33
  • @jmrodrigg use `@color/black` – offset Nov 19 '14 at 06:00
  • Actionbar behavior can be changed for api < 11. see http://stackoverflow.com/questions/8024706/how-do-i-change-the-background-color-of-the-actionbar-of-an-actionbaractivity-us/27847656#27847656 – Apurva Feb 06 '15 at 10:22
  • Can you help http://stackoverflow.com/questions/28609110/custom-theme-not-applying – Sasha Feb 19 '15 at 14:44
  • 1
    I'm not able to change text action bar text color using "@color/my_color" – Shirish Herwade Mar 05 '15 at 15:10
  • is it possible to set the color directly in "MyTheme" without creating the additional style tag? – wutzebaer Jan 14 '16 at 12:42
  • 2
    This didn't work for me with android:background tag but when I left out the android tag and put just the color changed for all platforms. – stevyhacker Jan 28 '16 at 00:15
  • `` and `` pair works for me. I'm using com.android.support:appcompat-v7:25.4.0. – Evi Song May 01 '18 at 08:32
  • It works, but now this changes my preferences screen background to the same color – A.W. Nov 27 '20 at 10:28
219

Try this

ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR"));
EdChum
  • 376,765
  • 198
  • 813
  • 562
coder_For_Life22
  • 26,645
  • 20
  • 86
  • 118
  • 1
    11-06 10:53:16.985: E/AndroidRuntime(16956): java.lang.NoSuchMethodError: com.acme.myActivity.getActionBar – user77115 Nov 06 '11 at 09:55
  • 1
    @coder_For_Life22 do you know how can I get a contextual action bar? – Geeks On Hugs Sep 01 '12 at 18:23
  • 6
    afaik, the support library itself doesn't have the ActionBar, for that you wil have to include the SherlockActionBar. – David Miler Oct 02 '12 at 15:53
  • 23
    maybe this is strange but in my case `bar.setBackgroundDrawable(new ColorDrawable(Color.BLUE));` change the action bar color to grey. These happens also if put other color, like `Color.RED`, `Color.GREEN` and so on... Does anybody have the same problem, or know the cause of this issue? I use a Nexus S device – AlexAndro Oct 16 '12 at 12:59
  • What if I want to align text to center?? `??` what could be the attribute at "??" – Mitesh Shah Jan 09 '14 at 07:56
  • 5
    http://stackoverflow.com/a/17198657/1022454 This post seems to fix the issue where the action bar color goes grey. – Jonathon Fry Jan 30 '14 at 14:41
  • in order to add a Hex color simply use Color.parseColor("#7bb42c"). – Kennedy Nyaga Mar 19 '14 at 19:37
  • @KennyWest Please do not edit an answer with code modifications, they should be comments – EdChum Mar 19 '14 at 19:44
  • @KennyWest **bar.setBackgroundDrawable(new ColorDrawable(Color.GREEN));** NULLPOINTEREXCEPTION? – Ruchir Baronia Nov 01 '15 at 20:56
127

try this:

ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
user2428604
  • 1,287
  • 1
  • 8
  • 2
69

Use this - http://jgilfelt.github.io/android-actionbarstylegenerator/

Its an amazing tool that lets you customize your actionbar with a live preview.

I tried the earlier answers but always had problems with changing other colors like the tabs and letters and spent hours tweaking stuff. This tool helped me get my design done in just a couple of minutes.

Here's a screenshot of the tool

All the best! :)

Mohammad Shabaz Moosa
  • 1,515
  • 1
  • 13
  • 20
58

I had the same problem, where my Action Bar would turn grey when I entered that code. Chances are your original style sheet looked like this:

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <!-- API 14 theme customizations can go here. -->
</style>

The "DarkActionBar" was what was keeping your Action Bar grey. I changed it to this, and it worked:

<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
    <!-- API 14 theme customizations can go here. -->
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">#2aa4cd</item>
    <item name="android:titleTextStyle">@style/Theme.MyAppTheme.ActionBar.TitleTextStyle</item>
</style>        

<style name="Theme.MyAppTheme.ActionBar.TitleTextStyle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">#FFFFFF</item>
</style>    

I also threw in how to edit the text color. Also, no need to change anything surrounding the resources.

-Warren

Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
Warren
  • 671
  • 6
  • 3
39

Behavior of Actionbar can also be changed in APIs < 11

See the Android Official Documentation for reference

I am building an app with minSdkVersion = "9" and targetSdkVersion = "21" I changed the color of action bar and it works fine with API level 9

Here is an xml

res/values/themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@color/actionbar_background</item>

        <!-- Support library compatibility -->
        <item name="background">@color/actionbar_background</item>
    </style>
</resources>

and set the color of actionbar you want

res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="actionbar_background">#fff</color> //write the color you want here
</resources>

Actionbar color can also be defined in .class file, the snippet is

ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));

but this will not work with the API < 11, so styling the actionbar in xml is only way for API < 11

Community
  • 1
  • 1
Apurva
  • 7,871
  • 7
  • 40
  • 59
21

For Kotlin users - I tried it and this works with light mode and dark mode -

    getSupportActionBar()?.setBackgroundDrawable(
             ColorDrawable(Color.parseColor("#003459"))
        
)

Add this in onCreate like this -

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        getSupportActionBar()?.setBackgroundDrawable(
             ColorDrawable(Color.parseColor("#003459"))
        )
CrazyMind
  • 1,006
  • 1
  • 20
  • 22
  • 2
    Would be better to use resources to get the color rather than having it as a text. `getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.black)));` – Dinesh Sep 29 '17 at 08:17
16

On the Nexus 4 people this seems to make the color go grey.

ActionBar bar = getActionBar(); // or MainActivity.getInstance().getActionBar()
bar.setBackgroundDrawable(new ColorDrawable(0xff00DDED));
bar.setDisplayShowTitleEnabled(false);  // required to force redraw, without, gray color
bar.setDisplayShowTitleEnabled(true);

(all credit to this post, but it is buried in the comments, so I wanted to surface it here) https://stackoverflow.com/a/17198657/1022454

Community
  • 1
  • 1
John Ballinger
  • 7,380
  • 5
  • 41
  • 51
14

try one line code :

getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.MainColor)));
SANAT
  • 8,489
  • 55
  • 66
  • 1
    My app crashes with this answer. To make it work in a fragment I changed it like this : getActivity().getActionBar().setBackgroundDrawable(new ColorDrawable(getActivity().getResources().getColor(R.color.col_nar))); , also tried getActivity().getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.col_nar))); . It crashes as soon as the fragment is opened. – Jose Manuel Abarca Rodríguez Apr 15 '15 at 22:53
  • 1
    Found the problem, it works with : ActionBar actionBar = getSupportActionBar(); – Jose Manuel Abarca Rodríguez Apr 15 '15 at 23:19
  • 1
    the answer is for the activity. If you want to use it with fragment then you have to use getActivity().getActionBar() for fragments – SANAT Apr 16 '15 at 08:45
9

2021: Kotlin oneliner with no deprecation:

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

Simply put it into onCreate and change the color depending on your needs

Merthan Erdem
  • 5,598
  • 2
  • 22
  • 29
6

This is how you can change the color of Action Bar.

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;


public class ActivityUtils {

public static void setActionBarColor(AppCompatActivity appCompatActivity, int colorId){
    ActionBar actionBar = appCompatActivity.getSupportActionBar();
    ColorDrawable colorDrawable = new ColorDrawable(getColor(appCompatActivity, colorId));
    actionBar.setBackgroundDrawable(colorDrawable);
}

public static final int getColor(Context context, int id) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 23) {
        return ContextCompat.getColor(context, id);
    } else {
        return context.getResources().getColor(id);
    }
}
}

From your MainActivity.java change the action bar color like this

    ActivityUtils.setActionBarColor(this, R.color.green_00c1c1);
Siddarth Kanted
  • 5,738
  • 1
  • 29
  • 20
5

When you are Extending Activity use following Code

ActionBar actionbar = getActionBar();
actionbar.setBackgroundDrawable(new ColorDrawable("color"));

When you are Extending AppCompatActivity use following Code

ActionBar actionbar = getSupportActionBar();
actionbar.setBackgroundDrawable(new ColorDrawable("color"));
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
arlo shie
  • 134
  • 1
  • 8
4

Sometimes this option throws NullPointerException

ActionBar actionbar = getActionBar(); actionbar.setBackgroundDrawable(new ColorDrawable("color"));

But this option is worked for me. So you can try this.

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

Happy Coding

Shahadat Hossain
  • 533
  • 7
  • 20
3

This worked for me, for AppCompatActivity,

    <item name="actionBarStyle">@style/BlackActionBar</item>
</style>

<style name="BlackActionBar" parent="@style/Widget.AppCompat.ActionBar.Solid">
    <item name="background">@android:color/black</item>
    <item name="titleTextStyle">@style/BlackActionBarTitleTextStyle</item>
</style>

<style name="BlackActionBarTitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:fontFamily">@font/cinzel_decorative</item>
</style>
Tejasvi Hegde
  • 2,694
  • 28
  • 20
3

If you are using androidx AppCompact. Use below code.

androidx.appcompat.app.ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable("Color"));
Dinith Rukshan Kumara
  • 638
  • 2
  • 10
  • 19
2

This code may be helpful

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
</style>
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- customize the color palette -->
    <item name="colorPrimary">@color/material_blue_500</item>
    <item name="colorPrimaryDark">@color/material_blue_700</item>
    <item name="colorAccent">@color/material_blue_500</item>
    <item name="colorControlNormal">@color/black</item>

</style>
<style name="CardViewStyle" parent="CardView.Light">
    <item name="android:state_pressed">@color/material_blue_700</item>
    <item name="android:state_focused">@color/material_blue_700</item>
    <!--<item name="android:background">?android:attr/selectableItemBackground</item>-->
</style>

Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
Qutbuddin Bohra
  • 1,165
  • 1
  • 11
  • 29
2

Its very simple for those who are using API 21 or greater Use this code below

for Dark ActionBar

<resources>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="android:background">@color/colorDarkGrey</item>
</style>

for_LightActionBar

<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.ActionBar">
    <item name="android:background">@color/colorDarkGrey</item>
</style>
Community
  • 1
  • 1
nirazverma
  • 1,001
  • 10
  • 15
1

Use This code ..to change action bar background color. open "res/values/themes.xml" (if not present, create it) and add

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
       parent="@android:style/Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
 <!-- ActionBar styles -->
<style name="MyActionBar"
       parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@drawable/actionbar_background</item>
</style>

Note : this code works for android 3.0 and higher versions only

Amey Bawiskar
  • 361
  • 1
  • 3
  • 17
0

Use this, it would work.

ActionBar actionbar = getActionBar();
actionbar.setBackgroundDrawable(new ColorDrawable("color"));
Pankaj Lilan
  • 4,245
  • 1
  • 29
  • 48
-2
getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.TabColor)));

color.xml file:

<resources> <color name="TabColor">#11FF00</color> </resources>`
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Chandresh Kachariya
  • 667
  • 2
  • 13
  • 31