Skip to content
This repository has been archived by the owner on Mar 19, 2022. It is now read-only.

Commit

Permalink
update Labels-support
Browse files Browse the repository at this point in the history
Ability to change Label Size, Label Margin-Top
  • Loading branch information
rakshakhegde committed Jan 30, 2017
1 parent 1675558 commit 22a525a
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 23 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.android.tools.build:gradle:2.3.0-beta3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Tue Sep 20 14:20:11 CEST 2016
#Thu Jan 26 15:22:41 IST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
8 changes: 4 additions & 4 deletions library/build.gradle
@@ -1,12 +1,12 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
compileSdkVersion 25
buildToolsVersion "25.0.2"

defaultConfig {
minSdkVersion 14
targetSdkVersion 23
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
Expand All @@ -24,7 +24,7 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:appcompat-v7:25.1.0'
}

apply from: 'gradle-mvn-push.gradle'
Expand Down
Expand Up @@ -21,6 +21,7 @@
import android.os.Parcelable;
import android.support.annotation.UiThread;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
Expand Down Expand Up @@ -272,14 +273,21 @@ public class StepperIndicator extends View implements ViewPager.OnPageChangeList

// X position of each step indicator's center
private float[] indicators;
// Utils to avoid object instanciation during onDraw
// Utils to avoid object instantiation during onDraw
private Rect stepAreaRect = new Rect();
private RectF stepAreaRectF = new RectF();

private ViewPager pager;
private Bitmap doneIcon;
private boolean showDoneIcon;

// If viewpager is attached, viewpager's page titles are used when {@code showLabels} equals true
private Paint labelPaint;
private CharSequence[] labels;
private boolean showLabels;
private float labelMarginTop;
private float labelSize;

// Running animations
private AnimatorSet animatorSet;
private ObjectAnimator lineAnimator, indicatorAnimator, checkAnimator;
Expand Down Expand Up @@ -529,6 +537,28 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr) {
animDuration = a.getInteger(R.styleable.StepperIndicator_stpi_animDuration, DEFAULT_ANIMATION_DURATION);
showDoneIcon = a.getBoolean(R.styleable.StepperIndicator_stpi_showDoneIcon, true);

// Labels Configuration
labelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
labelPaint.setTextAlign(Paint.Align.CENTER);

float defaultLabelSize = resources.getDimension(R.dimen.stpi_default_label_size);
labelSize = a.getDimension(R.styleable.StepperIndicator_stpi_labelSize, defaultLabelSize);
labelPaint.setTextSize(labelSize);

float defaultLabelMarginTop = resources.getDimension(R.dimen.stpi_default_label_margin_top);
labelMarginTop = a.getDimension(R.styleable.StepperIndicator_stpi_labelMarginTop, defaultLabelMarginTop);

showLabels(a.getBoolean(R.styleable.StepperIndicator_stpi_showLabels, false));
setLabels(a.getTextArray(R.styleable.StepperIndicator_stpi_labels));

if (isInEditMode() && showLabels && labels == null) {
labels = new CharSequence[]{"First", "Second", "Third", "Fourth", "Fifth"};
}

if (!a.hasValue(R.styleable.StepperIndicator_stpi_stepCount) && labels != null) {
setStepCount(labels.length);
}

a.recycle();

if (showDoneIcon) {
Expand Down Expand Up @@ -598,7 +628,10 @@ private void compute() {

float startX = circleRadius * EXPAND_MARK + circlePaint.getStrokeWidth() / 2f;
if (useBottomIndicator) {
startX += bottomIndicatorWidth / 2 - startX;
startX = bottomIndicatorWidth / 2F;
}
if (showLabels) {
startX = (getMeasuredWidth() / (float) stepCount) / 2F;
}

// Compute position of indicators and line length
Expand Down Expand Up @@ -680,8 +713,12 @@ private int getBottomIndicatorHeight() {
}
}

private float getLabelHeight() {
return showLabels ? labelSize + labelMarginTop : 0;
}

private float getStepCenterY() {
return (getMeasuredHeight() - getBottomIndicatorHeight()) / 2f;
return (getMeasuredHeight() - getBottomIndicatorHeight() - getLabelHeight()) / 2f;
}


Expand Down Expand Up @@ -729,6 +766,13 @@ protected void onDraw(Canvas canvas) {
canvas.drawText(stepLabel, stepAreaRectF.left, stepAreaRectF.top - stepTextNumberPaint.ascent(), stepTextNumberPaint);
}

if(showLabels && labels != null &&
i < labels.length && labels[i] != null) {
canvas.drawText(labels[i], 0, labels[i].length(),
indicator, getHeight() - getBottomIndicatorHeight() - labelPaint.descent(),
labelPaint);
}

if (useBottomIndicator) {
// Show the current step indicator as bottom line
if (i == currentStep) {
Expand Down Expand Up @@ -876,8 +920,12 @@ private boolean isStepValid(final int stepPos) {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Compute the necessary height for the widget
int desiredHeight = (int) Math.ceil((circleRadius * EXPAND_MARK * 2) + circlePaint.getStrokeWidth() +
getBottomIndicatorHeight());
int desiredHeight = (int) Math.ceil(
(circleRadius * EXPAND_MARK * 2)
+ circlePaint.getStrokeWidth()
+ getBottomIndicatorHeight()
+ getLabelHeight()
);

int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
Expand Down Expand Up @@ -1073,6 +1121,48 @@ public void setViewPager(ViewPager pager, int stepCount) {
this.stepCount = stepCount;
currentStep = 0;
pager.addOnPageChangeListener(this);

if(showLabels && labels == null) {
setLabelsUsingPageTitles();
}

invalidate();
}

private void setLabelsUsingPageTitles() {
PagerAdapter pagerAdapter = pager.getAdapter();
int pagerCount = pagerAdapter.getCount();
labels = new CharSequence[pagerCount];
for(int i = 0; i < pagerCount; i++)
labels[i] = pagerAdapter.getPageTitle(i);
}

/**
* Pass a labels array of Charsequence that is greater than or equal to the {@code stepCount}.
* Never pass {@code null} to this manually. Call {@code showLabels(false)} to hide labels.
*
* @param labelsArray Non-null array of CharSequence
*/
public void setLabels(CharSequence[] labelsArray) {
if (labelsArray == null) {
labels = null;
return;
}
if (stepCount > labelsArray.length) {
throw new IllegalArgumentException(
"Invalid number of labels for the indicators. Please provide a list " +
"of labels with at least as many items as the number of steps required!");
}
labels = labelsArray;
showLabels(true);
}

/**
* Shows the labels if true is passed. Else hides them.
* @param show Boolean to show or hide the labels
*/
public void showLabels(boolean show) {
showLabels = show;
invalidate();
}

Expand Down
6 changes: 6 additions & 0 deletions library/src/main/res/values/attrs.xml
Expand Up @@ -27,5 +27,11 @@

<attr format="reference" name="stpi_stepsCircleColors"/>
<attr format="reference" name="stpi_stepsIndicatorColors"/>


<attr format="reference" name="stpi_labels"/>
<attr format="boolean" name="stpi_showLabels"/>
<attr format="dimension" name="stpi_labelMarginTop"/>
<attr format="dimension" name="stpi_labelSize"/>
</declare-styleable>
</resources>
3 changes: 3 additions & 0 deletions library/src/main/res/values/dimens.xml
Expand Up @@ -11,4 +11,7 @@
<dimen name="stpi_default_bottom_indicator_height">3dp</dimen>
<dimen name="stpi_default_bottom_indicator_width">50dp</dimen>
<dimen name="stpi_default_bottom_indicator_margin_top">10dp</dimen>

<dimen name="stpi_default_label_size">12sp</dimen>
<dimen name="stpi_default_label_margin_top">2dp</dimen>
</resources>
12 changes: 6 additions & 6 deletions sample/build.gradle
Expand Up @@ -3,15 +3,15 @@ apply plugin: 'com.android.application'
def compileLocal = true

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
compileSdkVersion 25
buildToolsVersion "25.0.2"

defaultConfig {
applicationId "com.badoualy.stepperindicator.sample"
minSdkVersion 14
targetSdkVersion 23
versionCode 104
versionName "1.0.4"
targetSdkVersion 25
versionCode 105
versionName "1.1.0"
}
buildTypes {
release {
Expand All @@ -24,7 +24,7 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:appcompat-v7:25.1.0'

if (compileLocal)
compile project(':library')
Expand Down
Expand Up @@ -79,13 +79,17 @@ public EmptyPagerAdapter(FragmentManager fm) {

@Override
public int getCount() {
return 6;
return 5;
}

@Override
public Fragment getItem(int position) {
return PageFragment.newInstance(position + 1, position == getCount() - 1);
}

@Override
public CharSequence getPageTitle(int position) {
return "Page " + position;
}
}
}
3 changes: 1 addition & 2 deletions sample/src/main/res/layout/activity_main.xml
Expand Up @@ -17,8 +17,7 @@
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
app:stpi_showStepNumberInstead="true"
android:layout_marginTop="32dp"
app:stpi_stepCount="5"/>
app:stpi_labels="@array/stepLabels"/>

</RelativeLayout>
7 changes: 7 additions & 0 deletions sample/src/main/res/values/strings.xml
@@ -1,3 +1,10 @@
<resources>
<string name="app_name">Stepper Indicator</string>

<string-array name="stepLabels">
<item>Approval</item>
<item>Processed</item>
<item>Shipped</item>
<item>Delivery</item>
</string-array>
</resources>
2 changes: 1 addition & 1 deletion sample/src/main/res/values/styles.xml
@@ -1,7 +1,7 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
Expand Down
Expand Up @@ -7,7 +7,7 @@
/**
* To work on unit tests, switch the Test Artifact in the Build Variants view.
*/
public class ExampleUnitTest {
public class ExampleUnitTestSample {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
Expand Down

0 comments on commit 22a525a

Please sign in to comment.