Android tabs textcolor selector ignores state_pressed

884 views Asked by At

I have created my own android theme to change the look of the actionbar tabs. The problem is that textcolor selector seems to ignore state_pressed attribute, so the color of tab text is always the same, even if this tab is pressed. There is no problem with other states, for example state_selected is correctly recognized and selected tab has text color, which is different from unselected tabs text color.

What is more, i have also created selector for tabs background and it works fine with state_pressed (if the tab is pressed, it's backround color is changed).

There are some pieces of my code:

styles.xml:

<style name="Theme.MyTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarTabStyle">@style/Theme.MyTheme.TabStyle</item>
    <item name="android:actionBarTabTextStyle">@style/Theme.MyTheme.TabTextStyle</item>
</style>

...

<style name="Theme.MyTheme.TabStyle"
       parent="@android:style/Widget.Holo.Light.ActionBar.TabView">
    <item name="android:background">@drawable/background_selector</item>
</style>

<style name="Theme.MyTheme.TabTextStyle"
       parent="@android:style/Widget.Holo.Light.ActionBar.TabText">
    <item name="android:textColor">@color/textcolor_selector</item>
</style>

background_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="false" android:state_pressed="false">
        <shape>
            <solid android:color="#00ff00"/>
        </shape>
    </item>

    <item android:state_selected="false" android:state_pressed="true">
        <shape>
            <solid android:color="#0000ff"/>
        </shape>
    </item>

    <item android:state_selected="true" android:state_pressed="false">
        <shape>
            <solid android:color="#ff0000"/>
        </shape>
    </item>

    <item android:state_selected="true" android:state_pressed="true">
        <shape>
            <solid android:color="#ffff00"/>
        </shape>
    </item>
</selector>

textcolor_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:state_pressed="false"
          android:color="#ff0000"/>
    <item android:state_selected="true" android:state_pressed="true"
          android:color="#0000ff"/>
    <item android:state_selected="false" android:state_pressed="false"
          android:color="#ffff00"/>
    <item android:state_selected="false" android:state_pressed="true"
          android:color="#00ff00"/>
</selector>

I have tried everything without success - state_pressed attribute seems to be ignored, but only in textcolor_selector. Please, help me understand and solve this problem.

2

There are 2 answers

4
Bret On

Review the Customize the Text Color section of the ActionBar Styling documentation - it mentions that Note: The custom style applied to titleTextStyle should use TextAppearance.Holo.Widget.ActionBar.Title as the parent style. - perhaps a change there might help correct the problem.

Another place to look would be here under the Example theme section - it shows an example of android:actionBarTabTextStyle that might help get things sorted for you.

0
Wendel086 On

Try this in the drawable folder:

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

    <item android:state_enabled="true">
        <shape>
            <corners android:radius="0dp" />
            <solid android:color="@color/white" />
            <stroke android:width="1dp" android:color="@color/color_700" />
            <padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
        </shape>
    </item>
    <item android:state_enabled="false">
        <shape>
            <corners android:radius="0dp" />
            <solid android:color="@color/color_100" />
            <stroke android:width="1dp" android:color="@color/bloqueado" />
            <padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
        </shape>
    </item>

</selector>