Give text of selected tab a different color using ViewPagerIndicator

Bart Kiers picture Bart Kiers · May 15, 2013 · Viewed 13.7k times · Source

I've tried to let the SampleTabsStyled demo from the ViewPagerIndicator change the color of the text of the currently selected tab without success.

However, the SampleTitlesStyledTheme demo does change its text color when switching between titles/tabs.

When looking inside the styles xml:

<resources>
    ...
    <style name="CustomTitlePageIndicator">
        <item name="android:background">#18FF0000</item>
        <item name="footerColor">#FFAA2222</item>
        <item name="footerLineHeight">1dp</item>
        <item name="footerIndicatorHeight">3dp</item>
        <item name="footerIndicatorStyle">underline</item>
        <item name="android:textColor">#AA000000</item>
        <item name="selectedColor">#FF000000</item>
        <item name="selectedBold">true</item>
    </style>
    ...
    <style name="CustomTabPageIndicator" parent="Widget.TabPageIndicator">
        <item name="android:background">@drawable/custom_tab_indicator</item>
        <item name="android:textAppearance">@style/CustomTabPageIndicator.Text</item>
        <item name="android:textColor">#FF555555</item>
        <item name="android:textSize">16sp</item>
        <item name="android:divider">@drawable/custom_tab_indicator_divider</item>
        <item name="android:dividerPadding">10dp</item>
        <item name="android:showDividers">middle</item>
        <item name="android:paddingLeft">8dp</item>
        <item name="android:paddingRight">8dp</item>
        <item name="android:fadingEdge">horizontal</item>
        <item name="android:fadingEdgeLength">8dp</item>
    </style>

    <style name="CustomTabPageIndicator.Text" parent="android:TextAppearance.Medium">
        <item name="android:typeface">monospace</item>
    </style>
    ...
</resources>

I see that the SampleTitlesStyledTheme demo uses the CustomTitlePageIndicator style, which defines a selectedColor item. So (perhaps naively) I thought to add

<item name="selectedColor">#FF000000</item>

to the style the SampleTabsStyled demo is using, CustomTabPageIndicator, but, alas, that didn't work.

The question seems obvious, but I'll ask anyway: is there a way (using the present styles xml) to let the currently selected text of a tab in the SampleTabsStyled demo have a different color than the other tabs? If so, how?

EDIT

Oh, and I'm using this in combination with ActionBarSherlock, in case that is important...

Answer

Neoh picture Neoh · May 18, 2013

Assuming you don't mind creating an extra xml, first try include the attribute android:textColor in your CustomTabPageIndicator.Text (or CustomTabPageIndicator) like this:

<style name="CustomTabPageIndicator.Text" parent="android:TextAppearance.Medium">
    <item name="android:textColor">@drawable/tab_text_color</item>
    ...
</style>

where tab_text_color.xml is put under res/drawable folder:

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

Finally, just define the two colors @color/text_tab_selected and @color/text_tab_unselected in your color resource file colors.xml located in res/values folder (create one if it does not exist already). For example:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="text_tab_selected">#FF000000</color>
    <color name="text_tab_unselected">#FF555555</color>
</resources>