37

I'm trying to implement ActionBarSherlock because I was told it is relatively easy to implement and customize. I've found it was pretty easy to implement, but I'm trying to change the background color of the ActionBar and it's proving difficult.

According to the the site (link), it seems you can inherit one of the ActionBarSherlock's themes and then override the properties you need.

This is what I have so far:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <style name="Theme.ActionBar" parent="Theme.Sherlock.ForceOverflow">
      <item name="android:background">#000000</item>
      <item name="background">#000000</item>
    </style>
</resources>

I'm noticing the built-in theme are using images for the background, but I'm praying I don't have to create images to change the background color.

Thanks.

Kris B
  • 3,436
  • 9
  • 64
  • 106

3 Answers3

77

The action bar background color is defined in a style for the action bar, not in the theme itself. You'll need to do something like this:

<style name="Theme.MyTheme" parent="Theme.Sherlock.ForceOverflow">
    <item name="actionBarStyle">@style/Widget.MyTheme.ActionBar</item>
    <item name="android:actionBarStyle">@style/Widget.MyTheme.ActionBar</item>
</style>

<style name="Widget.MyTheme.ActionBar" parent="Widget.Sherlock.ActionBar">
    <item name="android:background">#ff000000</item>
    <item name="background">#ff000000</item>
</style>

Be careful using colors defined in XML. ColorDrawable did not respect it's view bounds on pre-Honeycomb so if you use tab navigation with a separate background for the stacked tab view you will have problems.

Jake Wharton
  • 75,598
  • 23
  • 223
  • 230
  • Thanks for the response. I'm not using tabs or anything, just a single ActionBar with some actions, so it's ok to use colors instead of images? I'm using ActionBarSherlock on 8 different apps and I don't have the expertise to create images. – Kris B Apr 08 '12 at 19:56
  • BTW, your code worked for adding the background color. Thanks. – Kris B Apr 08 '12 at 20:00
  • It's safe to use colors so long as you are not using it for the stacked background attribute. The regular background and split background will fine but on pre-3.0 using a color for stacked will not display properly. – Jake Wharton Apr 08 '12 at 23:59
  • Sounds good. Thanks for the responses and the great product. – Kris B Apr 09 '12 at 01:23
  • 3
    @JakeWharton `actionBarStyle` requires `API Level 11`. Is there a way to do it if targeting less than 11? Actually I just realized that only `android:actionBarStyle` requires 11, though this still doesn't seem to do anything. – theblang Dec 09 '13 at 21:13
  • Figured out the problem. Be sure to include the proper `value-v#` folder to apply styles to ranging API levels. See [this](http://stackoverflow.com/a/15339215/1747491) answer. – theblang Dec 09 '13 at 22:04
  • @JakeWharton I am developing with minimum api level of 9. It shows "actionBarStyle requires API level 11 (current min is 9)". It also shows "No resource found that matches the given name 'Theme.Sherlock.ForceOverflow'". What can i do help me out.. – ImMathan Dec 04 '14 at 12:43
  • @JakeWharton Hey what do you meant by "did not respect it's view bounds on pre-Honeycomb so if you use tab navigation with a separate background for the stacked tab view you will have problems." ? – Murtaza Khursheed Hussain Jan 19 '15 at 06:28
54

I just used

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

It changed the background color. Hope it helps.

Mahm00d
  • 3,881
  • 8
  • 44
  • 83
alicanbatur
  • 2,172
  • 1
  • 28
  • 36
2

The code mentioned by Jake Wharton is true. But when applying the code to the styles.xml may not work if you have minSDK<11 because android:actionBarStyle is supported in API-11+

To solve that error:

Make a folder of values-v11 in your res folder and create the XML file as mentioned above.

aagam94
  • 613
  • 1
  • 6
  • 20