Skip to content

Instantly share code, notes, and snippets.

@stemark
Last active March 27, 2020 11:16
Show Gist options
  • Save stemark/3ec7eef403f30a90e031 to your computer and use it in GitHub Desktop.
Save stemark/3ec7eef403f30a90e031 to your computer and use it in GitHub Desktop.
Use a progress bar to create a horizontal sliding tab line or a simple pagination index widget
<ProgressBar
android:id="@+id/paginate"
android:progressDrawable="@drawable/paginate_dot_bar"
android:layout_width="wrap_content"
android:layout_height="12dp"
android:layout_gravity="center"
android:paddingTop="3pt"
android:paddingBottom="0px"
android:clickable="false"
android:longClickable="false"
android:max="6"
android:progress="2"
android:secondaryProgress="3" />
<ProgressBar
android:id="@+id/paginate_bar"
android:progressDrawable="@drawable/paginate_bar"
android:layout_width="match_parent"
android:layout_height="12dp"
android:layout_gravity="center"
android:layout_marginLeft="0pt"
android:layout_marginRight="0pt"
android:paddingTop="3pt"
android:paddingBottom="0px"
android:layout_weight="0"
android:clickable="false"
android:longClickable="false"
android:max="6"
android:progress="2"
android:secondaryProgress="3" />
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/background">
<shape>
<solid android:color="@android:color/black"/>
</shape>
</item>
<!-- for pagination, the secondary is the main color -->
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<solid android:color="@android:color/white"/>
</shape>
</clip>
</item>
<!-- the primary is the background color to mask the out-of-low range values -->
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="@android:color/black"/>
</shape>
</clip>
</item>
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/background">
<bitmap
android:src="@drawable/paginate_dot"
android:tileMode="repeat" >
</bitmap>
</item>
<!-- for pagination, the secondary is the main color -->
<item android:id="@android:id/secondaryProgress">
<clip>
<bitmap
android:src="@drawable/paginate_dot_highlight"
android:tileMode="repeat" >
</bitmap>
</clip>
</item>
<!-- the primaray is the background color to mask the out-of-low range values -->
<item android:id="@android:id/progress">
<clip>
<bitmap
android:src="@drawable/paginate_dot"
android:tileMode="repeat" >
</bitmap>
</clip>
</item>
</layer-list>
package com.dangvarmit.helper
import android.view.ViewGroup;
import android.widget.ProgressBar;
/**
* Configures a standard ProgressBar to act like a horizontal page indexer
* can be configured to show dots or rectangles depending on the drawable used
* <p/>
* usage:
* PaginateBar paginator = new PaginateBar((ProgressBar) findViewById(R.id.paginator));
* ..
* paginate_dot.png
* paginate_dot_highlight.png are 25x25 px dots (grey&white). Size isn't critical but both images must be the same size
* paginator.setPageCount(int count);
* paginator.setPageIndex(int index);
* <p/>
*/
public class PaginateBar {
private final ProgressBar progress;
private final int pageDrawableWidth;
public PaginateBar (ProgressBar progress) {
this.progress = progress;
pageDrawableWidth = progress.getProgressDrawable().getIntrinsicWidth();
}
public int setPageCount (int count) {
progress.setMax(count);
// this should allow solid colors to be used
if (pageDrawableWidth > 0) {
ViewGroup.LayoutParams lp = progress.getLayoutParams();
lp.width = count * pageDrawableWidth;
progress.setLayoutParams(lp);
}
return count;
}
public int setPageIndex (int page) {
progress.setProgress(page);
progress.setSecondaryProgress(page + 1);
return page;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment