PagerSlidingTabStrip icon indicating copy to clipboard operation
PagerSlidingTabStrip copied to clipboard

is it possible set a active tab background color?

Open grantchen opened this issue 12 years ago • 10 comments

When a tab is actived, how to set the active tab background color?

Thanks

grantchen avatar Jun 08 '13 15:06 grantchen

I would also like to know this. Specifically, I would like to know if there's a way to set the active tab's text color instead of all the tabs' text colors

ghost avatar Jun 14 '13 22:06 ghost

I've implemented both of these changes in the following Gist: https://gist.github.com/derekbrameyer/5801412

This method uses ColorStateList and StateListDrawable to handle the text color and tab background. It still behaves correctly with integer colors and non-stateful drawables.

The changes are quite minimal (a diff of the file shows ~15 lines changed) and provide for better behavior.

@astuetz What do you think? Would definitely like to see these changes merged in :)

derekbrameyer avatar Jun 17 '13 23:06 derekbrameyer

what about a pull request to make it easier to merge ? ;)

Shusshu avatar Jun 24 '13 15:06 Shusshu

@Shusshu I downloaded your version with ColorStateList but I can't find where to change selected tab color... How do you change the color of the selected tab?

-- Edit -- I found a solution setting color from OnPageChangeListener.

leo7r avatar Dec 24 '13 21:12 leo7r

@Shusshu can you explain more what you did please?

Unaigo avatar Apr 22 '14 18:04 Unaigo

you have to use a color state list as background

http://developer.android.com/guide/topics/resources/color-list-resource.html

Shusshu avatar Apr 23 '14 07:04 Shusshu

I know it is late, but I would like to share how to set the active tab color in code, and hope this is helpful for the future reviewers... Suppose we would like set the active tab text color which is the same as the indicator color..

Update the updateTabStyles function

private void updateTabStyles() {

    for (int i = 0; i < tabCount; i++) {

        View v = tabsContainer.getChildAt(i);

        v.setBackgroundResource(tabBackgroundResId);

        if (v instanceof TextView) {

            TextView tab = (TextView) v;
            tab.setTextSize(TypedValue.COMPLEX_UNIT_PX, tabTextSize);
            tab.setTypeface(tabTypeface, tabTypefaceStyle);
            // here is the change
            if (i == pager.getCurrentItem()) {
                tab.setTextColor(getIndicatorColor());
            } else {
                tab.setTextColor(tabTextColor);
            }
            // end of the change

            // setAllCaps() is only available from API 14, so the upper case
            // is made manually if we are on a
            // pre-ICS-build
            if (textAllCaps) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                    tab.setAllCaps(true);
                } else {
                    tab.setText(tab.getText().toString()
                            .toUpperCase(locale));
                }
            }
        }
    }

Then update the scrollToChild method, and invoke the previews updateTabStyles..

private void scrollToChild(int position, int offset) {
    // add this line
    updateTabStyles();

    if (tabCount == 0) {
        return;
    }

    int newScrollX = tabsContainer.getChildAt(position).getLeft() + offset;

    if (position > 0 || offset > 0) {
        newScrollX -= scrollOffset;
    }

    if (newScrollX != lastScrollX) {
        lastScrollX = newScrollX;
        scrollTo(newScrollX, 0);
    }
}

zhouhaibing089 avatar Jun 18 '14 18:06 zhouhaibing089

@zhouhaibing089, what about tab background color?

trofimchyk avatar Jul 18 '14 07:07 trofimchyk

@trofimchyk I do not try that, but I suggest you can try with the same way, you may have noticed the method updateTabStyle just as above:

private void updateTabStyle() {
    for (...) {
        View v = tabsContainer.getChildAt(i);

        // try to modify this line of code..
        v.setBackgroundResource(tabBackgroundResId);
    }
}

zhouhaibing089 avatar Jul 19 '14 06:07 zhouhaibing089

I just done by,

Created selector drawable with
pager_heading_selector.xml android:state_selected="true" android:color="@color/red

and added into PagerSlidingTabStrip as app:pstsTabTextColor="@drawable/pager_heading_selector"

worked as i want..

SaravanaWorkspaces avatar Jun 24 '15 07:06 SaravanaWorkspaces