Set tabSelectedTextColor for TabLayout using selector

4k views Asked by At

Following codes work because I added the tabSelectedTextColor attribute directly and selected text color will be white.

<android.support.design.widget.TabLayout
            ...
            app:tabSelectedTextColor="@color/white"
            app:tabTextColor="@color/tab_layout"/>

But following codes don't work and I don't know why, maybe it is a bug!

<android.support.design.widget.TabLayout
            ...
            app:tabTextColor="@color/tab_layout"/>

@color/tab_layout

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Selected state defined so it's better to use it -->
    <item android:color="@color/white" android:state_selected="true"/>
    <item android:color="@color/white" android:state_focused="true"/>
    <item android:color="@color/white" android:state_pressed="true"/>
    <item android:color="#CCFFFFFF"/>
</selector>

Note: #CCFFFFFF color works so it means, the view gets the tabTextColor attribute value correctly but doesn't recognize the android:state_selected item. I tested all rational states but nothing worked.

TabLayout.class

Following codes copied from TabLayout.class and everything is clear. Don't you think getting selected text color from the selector is better way? If it is, please report it.

if(a.hasValue(styleable.TabLayout_tabSelectedTextColor)) {
    int selected = a.getColor(styleable.TabLayout_tabSelectedTextColor, 0);
    this.mTabTextColors = createColorStateList(this.mTabTextColors.getDefaultColor(), selected);
}
1

There are 1 answers

1
Amit Vaishnava On

If you want to change the selected text color then use setTabTextColors methode of TabLayout class like this:

tabLayout.setTabTextColors(Color.parseColor("#ADABAE"), Color.parseColor("#FFFFFF"));