1

I have Tab text which is blue and background is white. I want to show blue background and white tab text when a tab selected.

I changed the background by using a selector. But I tried multiple times text color is not changing when selected. Please help me on this.

styles.xml

<style name="MainTheme" parent="android:Theme.Holo.Light.DarkActionBar">   
    <item name="android:actionBarTabTextStyle">@style/tab_text_color</item>
</style>

<style name="actionbar_tab_style" parent="@android:style/Widget.Holo.Light.ActionBar.TabBar">
     <item name="android:background">@drawable/tab_background_select</item>
    <item name="android:textColor">@drawable/tab_text_select</item> 
</style>

tab_background_select.xml

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_selected="true"
        android:drawable="@color/blue"/>
</selector>

tab_text_select.xml

<?xml version="1.0" encoding="utf-8"?> 

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
   <item 
        android:state_selected="true"
        android:color="#FFFFFF"/>
    <item 
        android:state_selected="false"
        android:color="#0000FF"/>
      <!--   
        <item android:state_selected="true">
            <shape>
                <solid android:color="@color/white"/>
            </shape>
        </item>
        <item>
            <shape>
                <solid android:color="@color/light_blue"/>
            </shape>
        </item> -->

</selector>
tenten
  • 1,276
  • 2
  • 26
  • 54
  • I am assuming you are using `TabLayout`. Where are you defining your `TabLayout`? Please show the XML code so we can examine it. – ishmaelMakitla Jun 13 '16 at 08:52
  • I'm using action bar tabs in the code – tenten Jun 13 '16 at 08:53
  • I see, and were you aware that this way of navigation has been [deprecated in API level 21](https://developer.android.com/reference/android/app/ActionBar.Tab.html)? In any case, can you try something like: `getActionBar().setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#YourFavoriteColorHere")))` - also have a look at this closely related discussion [here](http://stackoverflow.com/questions/11318750/change-actionbar-tabs-background-color). – ishmaelMakitla Jun 13 '16 at 09:02
  • background is working fine I want change text color when selected only – tenten Jun 13 '16 at 09:40

1 Answers1

0

I found the answer. I had to put tab_text_select.xml in ti res/colors folder(If you don't have the folder just create it).

Then

<style name="actionbar_tab_text_style" parent="@android:style/Widget.Holo.Light.ActionBar.TabText">
 <item name="android:textColor">@color/tab_background_select</item>

Then apply above style to main theme name as android:actionBarTabTextStyle

tenten
  • 1,276
  • 2
  • 26
  • 54