PagerSlidingTabStrip icon indicating copy to clipboard operation
PagerSlidingTabStrip copied to clipboard

Can the text color be set differently for active and inactive tabs?

Open alexblack opened this issue 10 years ago • 22 comments

I'd like the currently selected tab to use white text, and the rest of the tabs to use grey.

I'm trying this but no luck, all the tabs text turns out white (which was unexpected)

    <com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:background="@color/deep_orange_500"
        app:pstsUnderlineColor="#fff"
        app:pstsIndicatorColor="#fff"
        android:textColor="@color/tab_text"
        android:layout_width="match_parent"
        android:layout_height="48dip" />
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true" android:color="#fff" android:drawable="@drawable/my_dummy_drawable"/>
    <item android:color="#aaa" android:drawable="@drawable/my_dummy_drawable"/>
</selector>

alexblack avatar Oct 27 '14 19:10 alexblack

Yes you can do it.Implement onPage Listener in your activity

private LinearLayout mTabsLinearLayout;

   @Override
public void onPageScrolled(int i, float v, int i2) {
    //overriden method not used
}

@Override
public void onPageSelected(int position) {
    for(int i=0; i < mTabsLinearLayout.getChildCount(); i++){
        TextView tv = (TextView) mTabsLinearLayout.getChildAt(i);
        if(i == position){
               tv.setTextColor(Color.WHITE);
        } else {
            tv.setTextColor(Color.GREY);
        }
    }
}

@Override
public void onPageScrollStateChanged(int i) {
         //overriden method not used
}

use this when your are setting up tabstrip when the activity is created..

      public void setUpTabStrip(){   

              //your other customizations related to tab strip...blahblah
              // Set first tab selected
    mTabsLinearLayout = ((LinearLayout)tabStrip.getChildAt(0));
    for(int i=0; i < mTabsLinearLayout.getChildCount(); i++){
        TextView tv = (TextView) mTabsLinearLayout.getChildAt(i);

        if(i == 0){
            tv.setTextColor(Color.WHITE);
        } else {
            tv.setTextColor(Color.GREY);
        }
    }

}

goonerDroid avatar Oct 31 '14 08:10 goonerDroid

Great, thanks for posting that.

Is there anyway to do that via XML? On Oct 31, 2014 1:27 AM, "goonerDroid" [email protected] wrote:

Yes you can do it.Implement onPage Listener in your activity

private LinearLayout mTabsLinearLayout;

@Override public void onPageScrolled(int i, float v, int i2) { //overriden method not used }

@Override public void onPageSelected(int position) { for(int i=0; i < mTabsLinearLayout.getChildCount(); i++){ TextView tv = (TextView) mTabsLinearLayout.getChildAt(i); if(i == position){ tv.setTextColor(Color.WHITE); } else { tv.setTextColor(Color.GREY); } } }

@Override public void onPageScrollStateChanged(int i) { //overriden method not used }

use this when your are setting up tabstrip when the activity is created..

  public void setUpTabStrip(){

          //your other customizations related to tab strip...blahblah
          // Set first tab selected
mTabsLinearLayout = ((LinearLayout)tabStrip.getChildAt(0));
for(int i=0; i < mTabsLinearLayout.getChildCount(); i++){
    TextView tv = (TextView) mTabsLinearLayout.getChildAt(i);

    if(i == 0){
        tv.setTextColor(Color.WHITE);
    } else {
        tv.setTextColor(Color.GREY);
    }
}

}

— Reply to this email directly or view it on GitHub https://github.com/astuetz/PagerSlidingTabStrip/issues/141#issuecomment-61231341 .

alexblack avatar Oct 31 '14 13:10 alexblack

NO...as far as i know the textview widget of the sliding tab strip cannot be modified using the XML.You would have to dynamically change the value on page selected by the user.Also close this issue if it solved your problem

goonerDroid avatar Nov 02 '14 06:11 goonerDroid

Thank you ! very useful to me !

xiaojidonggong avatar Nov 12 '14 07:11 xiaojidonggong

:+1: @goonerDroid for the sample code :)

pkpio avatar Nov 23 '14 01:11 pkpio

I advise to change block inside library (listener inside notifydatasetchanged() getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @SuppressWarnings("deprecation")
        @SuppressLint("NewApi")
        @Override
        public void onGlobalLayout() {

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                getViewTreeObserver().removeGlobalOnLayoutListener(this);
            } else {
                getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
            //modify previous tab to display inactive text
            TextView tab = (TextView)tabsContainer.getChildAt(currentPosition);
            tab.setTextColor(Color.parseColor("#50FFFFFF"));
            //modify previous tab to display active text
            currentPosition = pager.getCurrentItem();
             tab = (TextView) tabsContainer.getChildAt(currentPosition);
            tab.setTextColor(Color.parseColor("#FFFFFF"));
            scrollToChild(currentPosition, 0);
        }
    });

frakc avatar Dec 08 '14 12:12 frakc

@frakc This is better solution! Thanks :smile: :+1:

pkpio avatar Dec 08 '14 12:12 pkpio

#Update The previous method was unreliable.
Next solution will allow to set Colors from your main module, change them at run time and consumes less resourses, so sliding is smoother ###In activity or fragment:

    tabs.setTextColor(getResources().getColor(R.color.white_80));
    tabs.setActiveColor(getResources().getColor(R.color.white), getResources().getColor(R.color.white_80));
    tabs.setViewPager(pager);
    ```
###In Library add those field and setters:
```sh
private int activeTabTextColor;
    private int inActiveTabTextColor;
    private boolean useActiveColor =false;
    public void setActiveColor(int active, int inactive){
        useActiveColor=true;
        activeTabTextColor=active;
        inActiveTabTextColor=inactive;
    }
    public void disableActiveColor(){
        useActiveColor=false;
        }
        public void enableActiveClor(){
            useActiveColor = true;
        }
        ```
###Update setViewPager:
```sh
    public void setViewPager(ViewPager pager) {
            this.pager = pager;

            if (pager.getAdapter() == null) {
            throw new IllegalStateException("ViewPager does not have adapter instance.");
            }

            pager.setOnPageChangeListener(pageListener);

        notifyDataSetChanged();
        pageListener.curPage=currentPosition;
        pageListener.prevPage=currentPosition;
        if(useActiveColor){
            TextView tab  = (TextView)tabsContainer.getChildAt(currentPosition);
            tab.setTextColor(activeTabTextColor);
        }

    }
    ```
###Update Page Listener:
```sh
private class PageListener implements OnPageChangeListener {
        public int prevPage=0;
        public int curPage=0;
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            currentPosition = position;
            currentPositionOffset = positionOffset;

            scrollToChild(position, (int) (positionOffset * tabsContainer.getChildAt(position).getWidth()));

            invalidate();

            if (delegatePageListener != null) {
                delegatePageListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            if (state == ViewPager.SCROLL_STATE_IDLE) {
                scrollToChild(pager.getCurrentItem(), 0);
            }

            if (delegatePageListener != null) {
                delegatePageListener.onPageScrollStateChanged(state);
            }
        }

        @Override
        public void onPageSelected(int position) {
            prevPage=curPage;
            curPage=position;
            if(useActiveColor) {
                TextView tab = (TextView) tabsContainer.getChildAt(prevPage);
                tab.setTextColor(inActiveTabTextColor);
                //modify previous tab to display active text
                tab = (TextView) tabsContainer.getChildAt(curPage);
                tab.setTextColor(activeTabTextColor);
            }
            if (delegatePageListener != null) {
                delegatePageListener.onPageSelected(position);
            }
        }

    }
    ```

frakc avatar Dec 08 '14 13:12 frakc

@frakc any particular reason for using your solution.The code i'm using is working reliablily and so far there was no issues.Sorry if i being naive.

goonerDroid avatar Dec 30 '14 10:12 goonerDroid

@goonerDroid when i tried you sample my animation of tab changes was a bit of spiky, my way request a bit less redrowing, but it not very sutable as i see it now

frakc avatar Jan 16 '15 09:01 frakc

This worked for me, all in XML.

 <com.astuetz.PagerSlidingTabStrip
     android:id="@+id/tabs"
     android:layout_width="match_parent"
     android:layout_height="?attr/actionBarSize"
     android:background="?attr/colorPrimary"
     app:pstsTextColorSelected="@color/white"
     style="@style/PagerSlidingTabStripStyle"
     />

 <style name="PagerSlidingTabStripStyle" parent="">
     <item name="android:textColorPrimary">@color/gray</item>
 </style>

rockwotj avatar Mar 09 '15 12:03 rockwotj

Thanks @frakc it helped me , can u tell me if i reduce the height of the divider , my indicator color is say yellow and divider is white , while i swipe through i can see the White cut through the yellow , what can i do to change it , im thinking onDraw or use the divider padding attr - pstsDividerPadding, please advise

aldefy avatar Apr 01 '15 18:04 aldefy

Thanks @rockwotj !!! I solved this problem finally~!!! :D

baeggot avatar Jun 09 '15 02:06 baeggot

@goonerDroid thank you!

FerrisPan avatar Jun 14 '15 09:06 FerrisPan

I used @frakc 's solution but there is a bug. My default selected tab is always of the inactive colour. How do i make it the active colour?

simran-lbbd avatar Jun 16 '15 07:06 simran-lbbd

@rockwotj where did u find tag app:pstsTextColorSelected ? There is no this tag in library.

Tooto avatar Jun 16 '15 12:06 Tooto

@Tooto I just realized that I am using a fork of this project! Oops! facepalm

rockwotj avatar Jun 16 '15 13:06 rockwotj

This worked for me

<com.astuetz.PagerSlidingTabStrip
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:textColorPrimary="@color/white" //active tab color
            android:textColor="#cacaca" //inactive tab color
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" />

munnadroid avatar Jul 23 '15 07:07 munnadroid

@munnadroid thanks ur solution worked

pawan-nagarro avatar Jun 07 '16 17:06 pawan-nagarro

make following changes in library, worked for me!!

private void unSelect(View tab) {
        if (tab != null) {
            TextView tab_title = (TextView) tab.findViewById(R.id.psts_tab_title);
            tab_title.setTextColor(Color.GRAY);
            if (tab_title != null) {
                tab_title.setSelected(false);
            }
            if (isCustomTabs) ((CustomTabProvider) mPager.getAdapter()).tabUnselected(tab);
        }
    }

    private void select(View tab) {
        if (tab != null) {
            TextView tab_title = (TextView) tab.findViewById(R.id.psts_tab_title);
            tab_title.setTextColor(Color.WHITE);
            if (tab_title != null) {
                tab_title.setSelected(true);
            }
            if (isCustomTabs) ((CustomTabProvider) mPager.getAdapter()).tabSelected(tab);
        }
    }

ghost avatar Jul 15 '16 23:07 ghost

Is there a solution using only selector (From XML) ?

MohamedHatemAbdu avatar Aug 04 '16 10:08 MohamedHatemAbdu

Thanks.. !!! very nice solution !

Parkjonghyun93 avatar May 10 '17 04:05 Parkjonghyun93