4

I'm trying to change the colour of the action bar tabs programmatically. I have them by default in styles.xml as red, which is how I want them with the other tabs in my viewpager, however, on the first tab, I want both the action bar and the navigation tabs to become transparent. I've done this with the action bar by using this code

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    int newActionBarColor/*, newActionBarTabColor*/;
    if(tab.getPosition() == 0) {
        newActionBarColor  = Color.parseColor("#" + Integer.toHexString(getResources().getColor(R.color.actionBarTransparent)));
        //newActionBarTabColor = Color.parseColor("#" + Integer.toHexString(getResources().getColor(R.color.actionBarTabsTransparent)));
    } else {
        newActionBarColor  = Color.parseColor("#" + Integer.toHexString(getResources().getColor(R.color.actionBar)));
        //newActionBarTabColor = Color.parseColor("#" + Integer.toHexString(getResources().getColor(R.color.actionBarTabs)));
    }
    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(newActionBarColor));
    //getSupportActionBar().setStackedBackgroundDrawable(new ColorDrawable(newActionBarTabColor));
    //getSupportActionBar().setSplitBackgroundDrawable(new ColorDrawable(newActionBarTabColor));
    mViewPager.setCurrentItem(tab.getPosition());
}

But I can't get this working with the tabs, any ideas? You can see what I've tried above.

Activity on tab where I want coloured tabs:

What I have currently on the tab where I want both the action bar and tab to be transparent:

Ahmad
  • 69,608
  • 17
  • 111
  • 137
David Wood
  • 479
  • 6
  • 14

1 Answers1

0

When supporting Android 3.0 and higher only, you can define the action bar's background like this:

res/values/themes.xml
<?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>
</resources>

Then apply your theme to your entire app or individual activities:

<application android:theme="@style/CustomActionBarTheme" ... />

When using the Support Library, the same theme as above must instead look like this:

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">@drawable/actionbar_background</item>

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

Then apply your theme to your entire app or individual activities:

<application android:theme="@style/CustomActionBarTheme" ... />

You can get more information from the following link: Styling the Action Bar

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
  • This doesn't change anything and I don't know way. I copied to exactly and it keeps the action bar as the default color when I have it set to be red. –  Aug 20 '14 at 16:03
  • In the meantime I found the following link which may help you: `http://jgilfelt.github.io/android-actionbarstylegenerator/` – Michele La Ferla Aug 20 '14 at 16:42