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

Added function to change tab title/icon color when selected. #62

Closed
wants to merge 4 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
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Android PagerSlidingTabStrip
This is forked by [astuetz/PagerSlingTabStrip](https://github.com/astuetz/PagerSlidingTabStrip).

Interactive paging indicator widget, compatible with the `ViewPager` from the
Android Support Library.
Expand All @@ -7,6 +8,8 @@ Try out the sample application [on the Play Store](https://play.google.com/store

![PagerSlidingTabStrip Sample Screenshot 1](https://lh3.ggpht.com/PXS7EmHhQZdT1Oa379iy91HX3ByWAQnFZAthMAFa_QHAOHNClEaXU5nxDEAj1F2eqbk)![PagerSlidingTabStrip Sample Screenshot 2](https://lh3.ggpht.com/oaksDoUcQlGB4j7VEkBCOjrvSzjtzVHHcKq8pAnGVfm6oxkcJg_w1QS4tyP3fLcqrwcX)

![PagerSlidingTabStrip Sample Screenshot 3](https://dl.dropboxusercontent.com/u/18613868/Screenshot_2014-01-25-15-53-31.png)

# Usage

*For a working implementation of this project see the `sample/` folder.*
Expand Down Expand Up @@ -42,6 +45,37 @@ Try out the sample application [on the Play Store](https://play.google.com/store
// continued from above
tabs.setOnPageChangeListener(mPageChangeListener);

5. *(Optional)* If you want to `change tab title color` when selected
you should set attributes in the xml. **(Added)**

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dp"
app:pstsTabSwitch="true"
app:pstsActivateTextColor="#FF666666"
app:pstsDeactivateTextColor="#FFCCCCCC" />

...
</RelativeLayout>

6. *(Optional)* If you want to `change tab icon image` when selected
you should set pstsTabSwitch attribute in the xml and prepare `drawable selector`. **(Added)**

<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dp"
app:pstsTabSwitch="true" />

...

# Customization

To not just look like another Play Store styled app, go and adjust these values to match
Expand All @@ -58,6 +92,9 @@ your brand:
* `pstsTabBackground` Background drawable of each tab, should be a StateListDrawable
* `pstsShouldExpand` If set to true, each tab is given the same weight, default false
* `pstsTextAllCaps` If true, all tab titles will be upper case, default true
* `pstsTabSwitch` When a tab is selected, the drawable selector icon or the text color of the Tab title can be modified **(Added)**
* `pstsTextActivateColor` The Title color of selected Tab **(Added)**
* `pstsTextDeactivateColor` The Title color of unselected Tabs **(Added)**

*All attributes have their respective getters and setters to change them at runtime*

Expand Down
3 changes: 3 additions & 0 deletions library/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<attr name="pstsTabBackground" format="reference" />
<attr name="pstsShouldExpand" format="boolean" />
<attr name="pstsTextAllCaps" format="boolean" />
<attr name="pstsTabSwitch" format="boolean" />
<attr name="pstsActivateTextColor" format="color" />
<attr name="pstsDeactivateTextColor" format="color" />
</declare-styleable>

</resources>
3 changes: 3 additions & 0 deletions library/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="background_tab_pressed">#6633B5E5</color>
<color name="activate_text_color">#FF666666</color>
<color name="deactivate_text_color">#FFCCCCCC</color>
<color name="transparent">#00000000</color>
</resources>
72 changes: 59 additions & 13 deletions library/src/com/astuetz/PagerSlidingTabStrip.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.BaseSavedState;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.FrameLayout.LayoutParams;

import java.util.Locale;

Expand Down Expand Up @@ -85,19 +88,24 @@ public interface IconTabProvider {
private int underlineHeight = 2;
private int dividerPadding = 12;
private int tabPadding = 24;
private int dividerWidth = 1;
private int dividerWidth = 1;

private int tabTextSize = 12;
private int tabTextColor = 0xFF666666;
private int tabDeactivateTextColor = 0xFFCCCCCC;

private Typeface tabTypeface = null;
private int tabTypefaceStyle = Typeface.BOLD;

private int lastScrollX = 0;

private int tabBackgroundResId = R.drawable.background_tab;
private int transparentColorId = R.color.transparent;

private Locale locale;


private boolean tabSwitch;

public PagerSlidingTabStrip(Context context) {
this(context, null);
}
Expand Down Expand Up @@ -151,6 +159,9 @@ public PagerSlidingTabStrip(Context context, AttributeSet attrs, int defStyle) {
shouldExpand = a.getBoolean(R.styleable.PagerSlidingTabStrip_pstsShouldExpand, shouldExpand);
scrollOffset = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsScrollOffset, scrollOffset);
textAllCaps = a.getBoolean(R.styleable.PagerSlidingTabStrip_pstsTextAllCaps, textAllCaps);
tabSwitch = a.getBoolean(R.styleable.PagerSlidingTabStrip_pstsTabSwitch, tabSwitch);
tabTextColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsActivateTextColor, tabTextColor);
tabDeactivateTextColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsDeactivateTextColor, tabDeactivateTextColor);

a.recycle();

Expand Down Expand Up @@ -221,7 +232,6 @@ public void onGlobalLayout() {
scrollToChild(currentPosition, 0);
}
});

}

private void addTextTab(final int position, String title) {
Expand All @@ -230,19 +240,18 @@ private void addTextTab(final int position, String title) {
tab.setText(title);
tab.setGravity(Gravity.CENTER);
tab.setSingleLine();

addTab(position, tab);
}

private void addIconTab(final int position, int resId) {

ImageButton tab = new ImageButton(getContext());
tab.setImageResource(resId);

addTab(position, tab);

}

private void addTab(final int position, View tab) {
tab.setFocusable(true);
tab.setOnClickListener(new OnClickListener() {
Expand All @@ -253,23 +262,24 @@ public void onClick(View v) {
});

tab.setPadding(tabPadding, 0, tabPadding, 0);
tabsContainer.addView(tab, position, shouldExpand ? expandedTabLayoutParams : defaultTabLayoutParams);

tabsContainer.addView(tab, position, shouldExpand ? expandedTabLayoutParams : defaultTabLayoutParams);
}

private void updateTabStyles() {

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

View v = tabsContainer.getChildAt(i);

v.setBackgroundResource(tabBackgroundResId);

v.setBackgroundResource(!tabSwitch ? tabBackgroundResId : transparentColorId);
if (v instanceof TextView) {

TextView tab = (TextView) v;
tab.setTextSize(TypedValue.COMPLEX_UNIT_PX, tabTextSize);
tab.setTypeface(tabTypeface, tabTypefaceStyle);
tab.setTextColor(tabTextColor);
tab.setTextColor(tabSwitch && i != 0 ? tabDeactivateTextColor : tabTextColor);

// setAllCaps() is only available from API 14, so the upper case is made manually if we are on a
// pre-ICS-build
Expand All @@ -280,9 +290,26 @@ private void updateTabStyles() {
tab.setText(tab.getText().toString().toUpperCase(locale));
}
}
} else if (v instanceof ImageButton) {
ImageButton tab = (ImageButton) v;
tab.setSelected(tabSwitch && i == 0 ? true : false);
}
}
}

private void updateActivateTab(final int position) {

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

View v = tabsContainer.getChildAt(i);

if (v instanceof TextView) {
TextView tab = (TextView) v;
tab.setTextColor(position == i ? tabTextColor : tabDeactivateTextColor);
} else {
v.setSelected(position == i ? true : false);
}
}
}

private void scrollToChild(int position, int offset) {
Expand Down Expand Up @@ -380,13 +407,17 @@ public void onPageScrollStateChanged(int state) {

@Override
public void onPageSelected(int position) {
if (tabSwitch) {
updateActivateTab(position);
}

if (delegatePageListener != null) {
delegatePageListener.onPageSelected(position);
}
}

}

public void setIndicatorColor(int indicatorColor) {
this.indicatorColor = indicatorColor;
invalidate();
Expand Down Expand Up @@ -528,6 +559,21 @@ public int getTabPaddingLeftRight() {
return tabPadding;
}

public void setTabSwitch(boolean tabSwitch) {
this.tabSwitch = tabSwitch;
updateTabStyles();
}

public void setActivateTextColor(int activateTextColor) {
this.tabTextColor = activateTextColor;
updateTabStyles();
}

public void setDeactivateTextColor(int deactivateTextColor) {
this.tabDeactivateTextColor = deactivateTextColor;
updateTabStyles();
}

@Override
public void onRestoreInstanceState(Parcelable state) {
SavedState savedState = (SavedState) state;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions sample/res/drawable/selector_tab_1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/iconmonstr_sitemap_tab_1" />
<item android:state_pressed="true" android:drawable="@drawable/iconmonstr_sitemap_tab_1_selected"/>
<item android:state_selected="true" android:drawable="@drawable/iconmonstr_sitemap_tab_1_selected"/>
<item android:drawable="@drawable/iconmonstr_sitemap_tab_1" />
</selector>
7 changes: 7 additions & 0 deletions sample/res/drawable/selector_tab_2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/iconmonstr_sitemap_tab_2" />
<item android:state_pressed="true" android:drawable="@drawable/iconmonstr_sitemap_tab_2_selected"/>
<item android:state_selected="true" android:drawable="@drawable/iconmonstr_sitemap_tab_2_selected"/>
<item android:drawable="@drawable/iconmonstr_sitemap_tab_2" />
</selector>
7 changes: 7 additions & 0 deletions sample/res/drawable/selector_tab_3.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/iconmonstr_sitemap_tab_3" />
<item android:state_pressed="true" android:drawable="@drawable/iconmonstr_sitemap_tab_3_selected"/>
<item android:state_selected="true" android:drawable="@drawable/iconmonstr_sitemap_tab_3_selected"/>
<item android:drawable="@drawable/iconmonstr_sitemap_tab_3" />
</selector>
7 changes: 7 additions & 0 deletions sample/res/drawable/selector_tab_4.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/iconmonstr_sitemap_tab_4" />
<item android:state_pressed="true" android:drawable="@drawable/iconmonstr_sitemap_tab_4_selected"/>
<item android:state_selected="true" android:drawable="@drawable/iconmonstr_sitemap_tab_4_selected"/>
<item android:drawable="@drawable/iconmonstr_sitemap_tab_4" />
</selector>
3 changes: 2 additions & 1 deletion sample/res/layout-land/fragment_quick_contact.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
app:pstsDividerColor="#00000000"
app:pstsIndicatorColor="#FF33B5E6"
app:pstsTabPaddingLeftRight="14dip"
app:pstsUnderlineColor="#FF33B5E6" />
app:pstsUnderlineColor="#FF33B5E6"
app:pstsTabSwitch="true" />

<android.support.v4.view.ViewPager
android:id="@+id/pager"
Expand Down
3 changes: 2 additions & 1 deletion sample/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
android:background="@drawable/background_tabs" />
android:background="@drawable/background_tabs"
app:pstsTabSwitch="true" />

<android.support.v4.view.ViewPager
android:id="@+id/pager"
Expand Down
3 changes: 2 additions & 1 deletion sample/res/layout/fragment_quick_contact.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
app:pstsDividerColor="#00000000"
app:pstsIndicatorColor="#FF33B5E6"
app:pstsTabPaddingLeftRight="14dip"
app:pstsUnderlineColor="#FF33B5E6" />
app:pstsUnderlineColor="#FF33B5E6"
app:pstsTabSwitch="true" />

<android.support.v4.view.ViewPager
android:id="@+id/pager"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public class ContactPagerAdapter extends PagerAdapter implements IconTabProvider
private final int[] ICONS = { R.drawable.ic_launcher_gplus, R.drawable.ic_launcher_gmail,
R.drawable.ic_launcher_gmaps, R.drawable.ic_launcher_chrome };

private final int[] SWITCH_ICONS = { R.drawable.selector_tab_1, R.drawable.selector_tab_2,
R.drawable.selector_tab_3, R.drawable.selector_tab_4 };

public ContactPagerAdapter() {
super();
}
Expand All @@ -96,7 +99,8 @@ public int getCount() {

@Override
public int getPageIconResId(int position) {
return ICONS[position];
return SWITCH_ICONS[position];
// return ICONS[position];
}

@Override
Expand Down