2

How to set the background color of navigation tab in android with this kind of code. `

            ActionBar bar = getSupportActionBar();
            bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            ActionBar.Tab tab1 = bar.newTab();
            ActionBar.Tab tab2 = bar.newTab();
            tab1.setText("Fragment A");
            tab2.setText("Fragment B");
            tab1.setTabListener(new MyTabListener());
            tab2.setTabListener(new MyTabListener());
            bar.addTab(tab1);
            bar.addTab(tab2);`

i did not create a tab in xml. i was just wondering if its possible in this kind of code. thank you.

lhencq
  • 505
  • 2
  • 6
  • 16

3 Answers3

4

Did you try to style the tab background by changing tabbar style from res/values/themes.xml? Here is a sample for Theme.Sherlock.Light theme.

<style name="Theme.test" parent="@style/Theme.Sherlock.Light">
        <item name="android:actionBarTabBarStyle">@style/Theme.test.tabbar.style</item>
        <item name="actionBarTabBarStyle">@style/Widget.test.ActionBar.TabBar</item>
</style>

<style name="Theme.test.tabbar.style" parent="@style/Theme.Sherlock.Light.ActionBar">
    <item name="android:background">#00ff00</item>
    <item name="background">#00ff00</item>
</style>

<style name="Widget.test.ActionBar.TabBar" parent="Widget.Sherlock.Light.ActionBar.TabBar">
    <item name="android:background">#00ff00</item>
    <item name="background">#00ff00</item>
</style>

You need to set it twice because of HoneyComb and pre-HoneyComb devices. To use the new theme you will also need to add the following into the application tag in the manifest file.

android:theme="@style/Theme.test"
1

I'd start by checking out this other SO post: Android ActionBar Tab Color However, it seems like you can only set the whole background color & color of the bottom bar, not the color of each tab.

Otherwise, if you want to go old-skool, you could use a TabHost instead of an ActionBar. This'll allow you to set colors for each specific tab, and you should be able to style them similarly to the very 4.2ish ActionBar. 'Tis very simple:

tabHost.getTabWidget().getChildAt(TAB_1_INDEX).setBackgroundResource(R.drawable.tab_1_inactive);
tabHost.getTabWidget().getChildAt(TAB_2_INDEX).setBackgroundResource(R.drawable.tab_2_inactive);
tabHost.getTabWidget().getChildAt(TAB_3_INDEX).setBackgroundResource(R.drawable.tab_3_inactive);
tabHost.getTabWidget().getChildAt(pos).setBackgroundResource(activeTabShape[pos]);
Community
  • 1
  • 1
Stephen Wylie
  • 924
  • 7
  • 10
  • i'm actually planning to set the whole background to my tab. not per item/tab. :) – lhencq Jul 03 '13 at 03:18
  • Ah cool, then that SO page I referenced has a lot of good resources. You should check those out and then up-vote the one you like. ;) – Stephen Wylie Jul 03 '13 at 03:20
0

I am thinking to use ActionBar.Tab setCustomView (View view)

andy256
  • 2,821
  • 2
  • 13
  • 19