Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the text color of SlidingTabLayout?

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 !!!

like image 619
seba123neo Avatar asked Aug 01 '14 22:08

seba123neo


People also ask

Can we use TabLayout without ViewPager in Android?

It is possible to use a TabLayout without a ViewPager by using a TabLayout. OnTabSelectedListener . For navigation within an Activity , manually populate the UI based on the tab selected.


2 Answers

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);         }     }     
like image 63
Panayiotis Irakleous Avatar answered Sep 22 '22 02:09

Panayiotis Irakleous


Open your file SlidingTabLayout.java (the default one from Google IO) and find the function populateTabStrip() , then after this code

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

add the following line:

int color = ContextCompat.getColor(tabView.getContext(), R.color.grey); tabTitleView.setTextColor(color); 

Replace R.color.grey with your preferred color.

like image 37
Abhishek Balani Avatar answered Sep 25 '22 02:09

Abhishek Balani