Change color of navigation tabs programmatically

I am trying to change the color of the action bar tabs programmatically. I have the default in styles.xml as red, since I want them to be with different tabs in my view, however on the first tab I want the action bar and navigation tabs to become transparent. I did this using the action bar 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 not get this to work with tabs, any ideas? You can see what I tried above.

The activity on the tab where I want the color tabs:

What am I on the tab now, where I want the action bar and tab to be transparent:

+4
source share
1

Android 3.0 , :

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>

:

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

, , :

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>

:

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

:

0

Source: https://habr.com/ru/post/1523804/


All Articles