How to change the text color of SlidingTabLayout?

seba123neo picture seba123neo · Aug 2, 2014 · Viewed 17.4k times · Source

I made an application which use the ActionBarCompat

I created the tabs using the SlidingTabLayout class.

the class is this:

SlidingTabLayout.java

but I can not change the color of the tabs...

my viewpager fragment is this:

<swmovil.fyb.SlidingTabLayout
    android:id="@+id/mTabs"
    android:layout_width="match_parent"
    android:layout_height="48dip" />

<android.support.v4.view.ViewPager
    android:id="@+id/mPager"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="1"
    android:background="@color/white" />

the application works great, but i can´t change the color text of the tabs...

I made the application after seeing the following example:

rudsonlive/Navigation-Drawer-ViewPager-ActionBarCompat

How can i change the text color of the tabs text ?

thanks !!!

Answer

Panayiotis Irakleous picture Panayiotis Irakleous · Nov 16, 2014

1) First of all create color folder under res (/res/color)
2) create xml file selector.xml under /res/color 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="@android:color/white" />
<item android:state_focused="true" android:color="@android:color/white" />
<item android:state_pressed="true" android:color="@android:color/white" />
<item android:color="#504f4f" /> 
</selector> 

3) Then in the populateTabStrip() method in SlidingTabLayout put this

tabTitleView.setTextColor(getResources().getColorStateList(R.color.selector));

now you have a selector and you can change the color of the text on any event you want

if that is not working add the following lines of code.
a) in populateTabStrip() method at the end add this

if (i == mViewPager.getCurrentItem()) {
    tabView.setSelected(true);
}

and b) change the onPageSelected() method to this

    @Override
    public void onPageSelected(int position) {
        if (mScrollState == ViewPager.SCROLL_STATE_IDLE) {
            mTabStrip.onViewPagerPageChanged(position, 0f);
            scrollToTab(position, 0);
        }
        for (int i = 0; i < mTabStrip.getChildCount(); i++) {
            mTabStrip.getChildAt(i).setSelected(position == i);
        }
        if (mViewPagerPageChangeListener != null) {
            mViewPagerPageChangeListener.onPageSelected(position);
        }
    }