Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -13,8 +13,8 @@ class MainActivity : AppCompatActivity() {

findViewById<StepProgressBar>(R.id.stepProgressBar).apply {
max = 5
step = 1
stepDoneColor = Color.GREEN
step = 2
stepDoneColor = Color.BLUE
stepUndoneColor = Color.GRAY
}

Expand Down
16 changes: 13 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@

<kr.co.prnd.StepProgressBar
android:id="@+id/stepProgressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_width="300dp"
android:layout_height="10dp"
app:stepKeepMargin="true"
app:stepMargin="6dp"
android:layout_marginTop="24dp" />


<kr.co.prnd.StepProgressBar
android:id="@+id/stepProgressBar2"
android:layout_width="300dp"
android:layout_height="10dp"
android:layout_marginTop="24dp"
app:stepKeepMargin="false"
app:stepMargin="6dp" />


<kr.co.prnd.StepProgressBar
android:layout_width="300dp"
android:layout_height="8dp"
Expand All @@ -30,5 +41,4 @@
app:stepMargin="8dp"
app:stepUndoneColor="#dedede" />


</LinearLayout>
32 changes: 29 additions & 3 deletions stepprogressbar/src/main/java/kr/co/prnd/StepProgressBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class StepProgressBar @JvmOverloads constructor(
makeStepView()
}

var preserveStepMargin = false
set(value) {
field = value
makeStepView()
}

init {
orientation = HORIZONTAL
Expand All @@ -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()
}
}
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions stepprogressbar/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<attr name="stepDoneColor" format="color|reference" />
<attr name="stepUndoneColor" format="color|reference" />
<attr name="stepMargin" format="dimension" />
<attr name="stepKeepMargin" format="boolean" />
</declare-styleable>

</resources>