20

How to change color of tab when its selected, see below screen shot:

i am showing Orange color in ActionBar, in a same way i wanna show orange color in place of light blue.

To show Orange color in ActionBar background, i am using below code:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

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

   <style name="Theme.MyAppTheme.ActionBar" parent="android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">#FF4444</item>
    </style>

</resources>
Sonali
  • 783
  • 4
  • 11
  • 26
  • Read https://developer.android.com/training/basics/actionbar/styling.html and http://blog.alwold.com/2013/08/28/styling-tabs-in-the-android-action-bar/ – Pankaj Kumar Sep 20 '13 at 05:19

11 Answers11

16

I really recommend you to use the Actionbar Style Generator.

With that tool you can easily theme your graphic elements in your toolbar.

Mikael Olsson
  • 315
  • 3
  • 7
6

put this function and call it to yout Activity and pass tabhost as a parameter

public static void setTabColor(TabHost tabhost) {

            for (int i = 0; i < tabhost.getTabWidget().getChildCount(); i++) {
                tabhost.getTabWidget().getChildAt(i)
                        .setBackgroundResource(R.drawable.header_blank); // unselected
            }
            tabhost.getTabWidget().setCurrentTab(0);
            tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab())
                    .setBackgroundResource(R.drawable.tab_selected_new); // selected
                                                                            // //have
                                                                            // to
                                                                            // change
        }

call this as following way

        setTabColor(tabHost);
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {

            @Override
            public void onTabChanged(String arg0) {

                setTabColor(tabHost);
            }
             });

hope this is useful to you

Dhaval Patel
  • 694
  • 4
  • 12
3
myTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener(){
  @Override
  public void onTabChanged(String tabId) {
    int tab = myTabHost.getCurrentTab();
    View view = myTabHost.getTabWidget().getChildAt(tab).setBackgroundColor(Color.CYAN);
  }
});
Manjunath
  • 2,063
  • 2
  • 29
  • 60
2

use this code in order to change the color of selected tab:-

tabLayout.setTabTextColors(Color.parseColor("color_for_unselected_tab"), Color.parseColor("color_for_tab"));
for tab-indicator
 tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#627179")));
jrswgtr
  • 2,287
  • 8
  • 23
  • 49
Abhay Pratap
  • 1,886
  • 11
  • 15
1

To change tab bar background:

actionBar.setStackedBackgroundDrawable(new ColorDrawable(yourOwnColor)); 
DropAndTrap
  • 1,538
  • 16
  • 24
  • 1
    setStackedBackgroundDrawable changes the background for the whole bar. OP just wants the selected tab on action bar to be changed. – Amna Ali Jun 18 '14 at 20:05
1

Create an selector file which consist your desire color apply on tab xml like below:

 <?xml version="1.0" encoding="utf-8"?>
        <!-- 
        Copyright (c) Josh Clemm 2010
         -->
    <selector xmlns:android="http://schemas.android.com/apk/res/android">

        <!--  Active tab -->
        <item android:state_selected="true" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/dm_tab_highlight" />
        <!--  Inactive tab -->
    <!--    <item android:state_selected="false" android:state_focused="false" -->
    <!-- android:state_pressed="false" android:drawable="@drawable/tabbarbg" /> -->

        <!--  Pressed tab -->
        <item android:state_pressed="true" android:drawable="@drawable/dm_tab_highlight" />


    </selector> 
RobinHood
  • 10,897
  • 4
  • 48
  • 97
1

You can use this code and set icon alpha , it look like one is selected and other are disable.

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                tabLayoutUserProfileTabs.getTabAt(0).getIcon().setAlpha(255);
                tabLayoutUserProfileTabs.getTabAt(1).getIcon().setAlpha(128);
                tabLayoutUserProfileTabs.getTabAt(2).getIcon().setAlpha(128);
                tabLayoutUserProfileTabs.getTabAt(3).getIcon().setAlpha(128);
                break;
            case 1:
                tabLayoutUserProfileTabs.getTabAt(0).getIcon().setAlpha(128);
                tabLayoutUserProfileTabs.getTabAt(1).getIcon().setAlpha(255);
                tabLayoutUserProfileTabs.getTabAt(2).getIcon().setAlpha(128);
                tabLayoutUserProfileTabs.getTabAt(3).getIcon().setAlpha(128);
                break;
            case 2:
                tabLayoutUserProfileTabs.getTabAt(0).getIcon().setAlpha(128);
                tabLayoutUserProfileTabs.getTabAt(1).getIcon().setAlpha(128);
                tabLayoutUserProfileTabs.getTabAt(2).getIcon().setAlpha(255);
                tabLayoutUserProfileTabs.getTabAt(3).getIcon().setAlpha(128);
                break;
            case 3:
                tabLayoutUserProfileTabs.getTabAt(0).getIcon().setAlpha(128);
                tabLayoutUserProfileTabs.getTabAt(1).getIcon().setAlpha(128);
                tabLayoutUserProfileTabs.getTabAt(2).getIcon().setAlpha(128);
             tabLayoutUserProfileTabs.getTabAt(3).getIcon().setAlpha(255);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
});
1

// you can change tab text and indicator using this

<com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabTextColor="@color/gray2"
        app:tabSelectedTextColor="@color/orange2"
        app:tabIndicatorColor="@color/orange2"/>
</com.google.android.material.appbar.AppBarLayout>
0

you can use like this

tab_background_select.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
        android:drawable="@drawable/tab_background" />// for selected
     <item android:drawable="@drawable/tab" /> // for normal
</selector>
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
0

you can use this code and set the background of your item_tab xml file

tab_selection.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:state_pressed="false" 
      android:drawable="@drawable/tab_bg_selected" />
<item android:state_selected="false" android:state_pressed="false" 
      android:drawable="@drawable/tab_bg_unselected" />
<item android:state_pressed="true" 
      android:drawable="@drawable/tab_bg_pressed" />
</selector>
Vishwa
  • 251
  • 3
  • 6
0

Use this line app:tabSelectedTextColor="@color/colorPrimaryDark" in your xml

<com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:tabMode="scrollable"
        android:layout_alignParentBottom="true"
        app:tabSelectedTextColor="@color/colorPrimaryDark"
        app:tabIndicatorColor="@color/colorPrimaryDark"

        >
    </com.google.android.material.tabs.TabLayout>
Abdul Basit Rishi
  • 2,268
  • 24
  • 30