Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove selected text color attribute #70

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions library/res/color-v21/default_tab_text.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?android:textColorPrimary" android:state_selected="true" />
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't ?android:textColorPrimary available in pre L releases?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right, it's available. Doesn't look like it can be used as a color attribute though, weirdly - it doesn't resolve to the correct color (the resource ID gets parsed as the color, rather than resolving it). Using it as a drawable resource, like for a background, seems to work. So confused.

<item android:color="#80FFFFFF" />
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not going to be able to generate android:textColorPrimary with 150 alpha this way. So we need to rely in half transparent white. That´s another downside. It is nice to have default colours as your theme.

</selector>
5 changes: 5 additions & 0 deletions library/res/color/default_tab_text.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FFFFFF" android:state_selected="true" />
<item android:color="#80FFFFFF" />
</selector>
4 changes: 1 addition & 3 deletions library/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
<attr name="pstsShouldExpand" format="boolean" />
<attr name="pstsTextAllCaps" format="boolean" />
<attr name="pstsPaddingMiddle" format="boolean" />
<attr name="pstsTextColorSelected" format="color" />
<attr name="pstsTextAlpha" format="integer" />
<attr name="pstsTextStyle">
<flag name="normal" value="0x0" />
<flag name="bold" value="0x1" />
Expand All @@ -29,4 +27,4 @@
</attr>
</declare-styleable>

</resources>
</resources>
44 changes: 19 additions & 25 deletions library/src/com/astuetz/PagerSlidingTabStrip.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Typeface;
Expand Down Expand Up @@ -58,7 +57,7 @@ public interface OnTabReselectedListener {
}

// @formatter:off
private static final int[] ATTRS = new int[]{
private static final int[] ANDROID_ATTRS = new int[]{
android.R.attr.textColorPrimary,
android.R.attr.textSize,
android.R.attr.textColor,
Expand Down Expand Up @@ -109,8 +108,6 @@ public interface OnTabReselectedListener {
private int tabPadding = 12;
private int tabTextSize = 14;
private ColorStateList tabTextColor = null;
private ColorStateList tabTextColorSelected = null;
private int textAlpha = 150;

private int paddingLeft = 0;
private int paddingRight = 0;
Expand Down Expand Up @@ -157,24 +154,27 @@ public PagerSlidingTabStrip(Context context, AttributeSet attrs, int defStyle) {
tabTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, tabTextSize, dm);

// get system attrs (android:textSize and android:textColor)
TypedArray a = context.obtainStyledAttributes(attrs, ATTRS);
TypedArray a = context.obtainStyledAttributes(attrs, ANDROID_ATTRS);
tabTextSize = a.getDimensionPixelSize(TEXT_SIZE_INDEX, tabTextSize);
ColorStateList colorStateList = a.getColorStateList(TEXT_COLOR_INDEX);

if (a.hasValue(TEXT_COLOR_INDEX)) {
tabTextColor = a.getColorStateList(TEXT_COLOR_INDEX);
} else {
tabTextColor = getResources().getColorStateList(R.color.default_tab_text);
}

int textPrimaryColor = a.getColor(TEXT_COLOR_PRIMARY, android.R.color.white);

underlineColor = textPrimaryColor;
dividerColor = textPrimaryColor;
indicatorColor = textPrimaryColor;
int padding = a.getDimensionPixelSize(PADDING_INDEX, 0);
paddingLeft = padding > 0 ? padding : a.getDimensionPixelSize(PADDING_LEFT_INDEX, 0);
paddingRight = padding > 0 ? padding : a.getDimensionPixelSize(PADDING_RIGHT_INDEX, 0);
a.recycle();

// get custom attrs
a = context.obtainStyledAttributes(attrs, R.styleable.PagerSlidingTabStrip);
indicatorColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsIndicatorColor, indicatorColor);
underlineColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsUnderlineColor, underlineColor);
dividerColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsDividerColor, dividerColor);
indicatorColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsIndicatorColor, textPrimaryColor);
underlineColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsUnderlineColor, textPrimaryColor);
dividerColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsDividerColor, textPrimaryColor);
dividerWidth = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsDividerWidth, dividerWidth);
indicatorHeight = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsIndicatorHeight, indicatorHeight);
underlineHeight = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsUnderlineHeight, underlineHeight);
Expand All @@ -187,17 +187,8 @@ public PagerSlidingTabStrip(Context context, AttributeSet attrs, int defStyle) {
isPaddingMiddle = a.getBoolean(R.styleable.PagerSlidingTabStrip_pstsPaddingMiddle, isPaddingMiddle);
tabTypefaceStyle = a.getInt(R.styleable.PagerSlidingTabStrip_pstsTextStyle, Typeface.BOLD);
tabTypefaceSelectedStyle = a.getInt(R.styleable.PagerSlidingTabStrip_pstsTextSelectedStyle, Typeface.BOLD);
tabTextColorSelected = a.getColorStateList(R.styleable.PagerSlidingTabStrip_pstsTextColorSelected);
textAlpha = a.getInt(R.styleable.PagerSlidingTabStrip_pstsTextAlpha, textAlpha);
a.recycle();

tabTextColor = colorStateList == null ? getColorStateList(Color.argb(textAlpha,
Color.red(textPrimaryColor),
Color.green(textPrimaryColor),
Color.blue(textPrimaryColor))) : colorStateList;

tabTextColorSelected = tabTextColorSelected == null ? getColorStateList(textPrimaryColor) : tabTextColorSelected;

setMarginBottomTabContainer();

rectPaint = new Paint();
Expand Down Expand Up @@ -278,8 +269,11 @@ private void addTab(final int position, CharSequence title, View tabView) {
TextView textView = (TextView) tabView.findViewById(R.id.tab_title);
if (textView != null) {
if (title != null) textView.setText(title);
ColorStateList color = pager.getCurrentItem() == position ? tabTextColorSelected : tabTextColor;
textView.setTextColor(color);

if (pager.getCurrentItem() == position) {
textView.setSelected(true);
}
textView.setTextColor(tabTextColor);
}

tabView.setFocusable(true);
Expand Down Expand Up @@ -506,7 +500,7 @@ private void notSelected(View tab) {
TextView title = (TextView) tab.findViewById(R.id.tab_title);
if (title != null) {
title.setTypeface(tabTypeface, tabTypefaceStyle);
title.setTextColor(tabTextColor);
title.setSelected(false);
}
}
}
Expand All @@ -516,7 +510,7 @@ private void selected(View tab) {
TextView title = (TextView) tab.findViewById(R.id.tab_title);
if (title != null) {
title.setTypeface(tabTypeface, tabTypefaceSelectedStyle);
title.setTextColor(tabTextColorSelected);
title.setSelected(true);
}
}
}
Expand Down