13

Is there a way to change the background color of the tab bar in the ActionBar without changing it in the one line version?

To clarify what I want: In portrait mode the ActionBar is split in two lines, the ActionBar itself and the tabs below. In landscape mode the tabs are in the actual ActionBar.

I want to change the background color of the portrait mode. If I change the background in the TabView it'll be changed for both modes. Do I have to create separate styles for those? Which brings up a second question: is there a way to know when it'll be two lines and when not?

Or am I just missing something?

I'm using ActionBarSherlock btw

Maria Neumayer
  • 3,337
  • 3
  • 27
  • 44

5 Answers5

45

I think you are looking for the android:backgroundStacked attribute of the ActionBar style:

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

<style name="MyActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar.Solid">
    <item name="android:backgroundStacked">@drawable/my_stacked_background</item>
</style>

or (If using ActionBarSherlock):

<style name="MyTheme" parent="@style/Theme.Sherlock.Light">
    <item name="android:actionBarStyle">@style/MyActionBarStyle</item>
    <item name="actionBarStyle">@style/MyActionBarStyle</item>
</style>

<style name="MyActionBarStyle" parent="@style/Widget.Sherlock.Light.ActionBar.Solid">
    <item name="android:backgroundStacked">@drawable/my_stacked_background</item>
    <item name="backgroundStacked">@drawable/my_stacked_background</item>
</style>
Maria Neumayer
  • 3,337
  • 3
  • 27
  • 44
JesperB
  • 4,625
  • 1
  • 36
  • 40
  • 1
    Can you elaborate on how to make this work with the default holo light theme? What do you name it? What do you call in the manifest? This doesn't work. –  Aug 20 '14 at 14:25
  • @JesperB why we need to set both "android:backgroundStacked" and "backgroundStacked"? –  May 09 '18 at 13:48
23

To Change Actionbar Tab's color, Pls use this code:

//For Example if you want White color as Tabs background, then

getActionBar().setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));
Srikanth P
  • 1,506
  • 14
  • 12
  • This is the one answer which helped me after day. Styles are too complicated. This is the easiest way. – Nitish Mar 27 '15 at 13:39
  • Why `mActionBar.setStackedBackgroundDrawable(new ColorDrawable(R.color.action_bar_bg));` does not work? – Braj Oct 01 '15 at 11:08
3

In ActionBarSherlock's values/abs__themes.xml there is

<item name="actionModeSplitBackground">@drawable/abs__cab_background_bottom_holo_dark</item>

You have to create your own theme derived from ABS

<style name="AppTheme" parent="Theme.Sherlock">
    <item name="actionBarStyle">@style/MyActionBar</item>
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="Widget.Sherlock.ActionBar">
    <item name="actionModeSplitBackground">@drawable/my_split_background</item>
</style>

Hope this helps you.

vasart
  • 6,692
  • 38
  • 39
  • That's what I thought it might be, but it doesn't change anything. – Maria Neumayer Jul 03 '12 at 20:59
  • Have you applied this theme to your application in AndroidManifest.xml? – vasart Jul 03 '12 at 21:15
  • Yes. But it's the wrong value. If you look at the actual asset (abs__cab_background_bottom_holo_dark) the blue bar that's being used to show that it's selected is on top, not on bottom. Also there are no different modes for selected/unselected etc – Maria Neumayer Jul 03 '12 at 21:18
  • Ok, there is also `@drawable/abs__ab_bottom_transparent_dark_holo` in `values/abs__styles.xml`. I don't remember the exact item name, but you have to look in that direction – vasart Jul 03 '12 at 21:23
  • 3
    It's backgroundStacked! Thanks for guiding me into the right direction. – Maria Neumayer Jul 03 '12 at 21:33
1
ColorDrawable colorDrawable = new ColorDrawable(Color.White);
actionBar.SetStackedBackgroundDrawable(colorDrawable);

for xamarin folks.

megaKertz
  • 484
  • 6
  • 11
0

To seperate styles depending on orientation you have to create in your /res folder a new folder called layout-land (for landscape mode) and layout-port (in portrait mode) and put your xml files for action bar and set it's specific style (with the color you want) on each folder.

Mouna Cheikhna
  • 38,870
  • 10
  • 48
  • 69
  • Thanks, I'm aware of that. But it's not just landscape/portrait. I assume on tablets it'll always be just one line. I know I can also have separate styles for large screens etc but I don't know what mode will be used in which configuration. – Maria Neumayer Jul 03 '12 at 20:56