is it possible set a active tab background color?
When a tab is actived, how to set the active tab background color?
Thanks
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
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 :)
what about a pull request to make it easier to merge ? ;)
@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.
@Shusshu can you explain more what you did please?
you have to use a color state list as background
http://developer.android.com/guide/topics/resources/color-list-resource.html
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, what about tab background color?
@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);
}
}
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..