From 588b245bb5cf6c39243252065a68bb64b07ef80d Mon Sep 17 00:00:00 2001 From: Shido Date: Thu, 6 Feb 2020 12:21:29 -0300 Subject: [PATCH 1/2] Added Margin Feature --- .../example/stepprogressbardemo/MainActivity.kt | 6 +++--- app/src/main/res/layout/activity_main.xml | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/example/stepprogressbardemo/MainActivity.kt b/app/src/main/java/com/example/stepprogressbardemo/MainActivity.kt index e0cfd13..9864040 100644 --- a/app/src/main/java/com/example/stepprogressbardemo/MainActivity.kt +++ b/app/src/main/java/com/example/stepprogressbardemo/MainActivity.kt @@ -1,8 +1,8 @@ package com.example.stepprogressbardemo import android.graphics.Color -import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity import kr.co.prnd.StepProgressBar class MainActivity : AppCompatActivity() { @@ -13,8 +13,8 @@ class MainActivity : AppCompatActivity() { findViewById(R.id.stepProgressBar).apply { max = 5 - step = 1 - stepDoneColor = Color.GREEN + step = 2 + stepDoneColor = Color.BLUE stepUndoneColor = Color.GRAY } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index e833498..2e2723a 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -8,11 +8,22 @@ + + + - \ No newline at end of file From afa651ab8190dd4e7a00979df4f04a2a39ed4a18 Mon Sep 17 00:00:00 2001 From: Shido Date: Thu, 6 Feb 2020 12:21:55 -0300 Subject: [PATCH 2/2] Added Margin Feature --- .../main/java/kr/co/prnd/StepProgressBar.kt | 32 +++++++++++++++++-- stepprogressbar/src/main/res/values/attr.xml | 1 + 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/stepprogressbar/src/main/java/kr/co/prnd/StepProgressBar.kt b/stepprogressbar/src/main/java/kr/co/prnd/StepProgressBar.kt index b6ee639..0bbe567 100644 --- a/stepprogressbar/src/main/java/kr/co/prnd/StepProgressBar.kt +++ b/stepprogressbar/src/main/java/kr/co/prnd/StepProgressBar.kt @@ -50,6 +50,11 @@ class StepProgressBar @JvmOverloads constructor( makeStepView() } + var preserveStepMargin = false + set(value) { + field = value + makeStepView() + } init { orientation = HORIZONTAL @@ -69,6 +74,9 @@ class StepProgressBar @JvmOverloads constructor( stepMargin = typedArray.getDimensionPixelSize(R.styleable.StepProgressBar_stepMargin, stepMargin) + preserveStepMargin = + typedArray.getBoolean(R.styleable.StepProgressBar_stepKeepMargin, false) + typedArray.recycle() } } @@ -101,15 +109,33 @@ class StepProgressBar @JvmOverloads constructor( removeAllViewsInLayout() - val totalViewWidth = width - stepMargin * (max - 1) + val widthWithoutMargin = width - stepMargin + val totalViewWidth = widthWithoutMargin - stepMargin * (max - 1) val undoneViewWidth = totalViewWidth / max val undoneStepCount = max - step - val doneViewWidth = width - undoneStepCount * (undoneViewWidth + stepMargin) - addDoneView(doneViewWidth, height) + if (preserveStepMargin) { + val doneViewWidth = totalViewWidth / max + repeat(step) { addDoneViewMargin(doneViewWidth, height) } + + } else { + val doneViewWidth = width - undoneStepCount * (undoneViewWidth + stepMargin) + addDoneView(doneViewWidth, height) + } + repeat(undoneStepCount) { addUndoneView(undoneViewWidth, height) } } + + private fun addDoneViewMargin(doneViewWidth: Int, height: Int) { + addView(View(context).apply { + layoutParams = LayoutParams(doneViewWidth, height) + .apply { leftMargin = stepMargin } + setBackgroundColor(stepDoneColor) + }) + } + + private fun addDoneView(doneViewWidth: Int, height: Int) { addView(View(context).apply { layoutParams = LayoutParams(doneViewWidth, height) diff --git a/stepprogressbar/src/main/res/values/attr.xml b/stepprogressbar/src/main/res/values/attr.xml index 9a306d2..4de7810 100644 --- a/stepprogressbar/src/main/res/values/attr.xml +++ b/stepprogressbar/src/main/res/values/attr.xml @@ -7,6 +7,7 @@ + \ No newline at end of file