Search code examples
androidandroiddesignsupportpagerslidingtabstripandroid-tablayout

Changing Text Color of a Particular Tab in PagerSlidingTabStrip


i am using PagerSlidingTabStrip to implement 3 tabs inside a view pager and everything seems fine. my question is i have searched but couldnt find the right answer of how to change the text color of a particular tab at a certain event, and when the user navigates to that tab the text color is back to normal (when selected and not selected). i don't mind using a TabLayout if this is not supported by PagerSlidingTabStrip. please any tutorials or guides to help me?


Solution

  • I customize SlidingTabLayout for this purpose. Go through this link SlidingTabLayout - gist. or If you don't want to change your PagerSlidingTabStrip with SlidingTabLayout then, you need to customize your onPageSelected() of your PagerSlidingTabStrip like getting the position of the tab. Then find your TextView in ViewHierarchy and change TextView's textColor.

    if((position == i)){
        tabPosition = position;
        TextView title = (TextView) mTabStrip.getChildAt(i);
        title.setTextColor(mTabViewSelectedTextColor);
    } else{
        TextView title = (TextView) mTabStrip.getChildAt(i);
        title.setTextColor(mTabViewTextColor);
    }
    

    The above mentioned SlidingTabLayout (gist) link will give output like this.

    enter image description here