Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 3fd1a46

Browse files
juliooaimaNNeo
authored andcommitted
[Feature] add steps name under step (#3)
* show step name boolean attribute * logic to add stepNames under the steps * sample code for steps name under the step * decreasing fontsize and removing not-related attributes * default showStepNames to false * adding comments * replaced setStepName function with StepsTitleSetter interface * removed last stepBarView and applied showStep to first
1 parent 96ac2a8 commit 3fd1a46

4 files changed

Lines changed: 89 additions & 20 deletions

File tree

app/src/main/java/ir/neo/stepbarviewdemo/MainActivity.kt

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import android.view.View
66
import android.widget.*
77
import ir.neo.stepbarview.DpHandler
88
import ir.neo.stepbarview.StepBarView
9+
import ir.neo.stepbarview.StepBarView.StepsTitleSetter
10+
import kotlinx.android.synthetic.main.activity_main.*
911

1012
class MainActivity : AppCompatActivity() ,SeekBar.OnSeekBarChangeListener{
11-
lateinit var myStepBarView : StepBarView
1213
lateinit var sbAttrsValue: SeekBar
1314
lateinit var spActions : Spinner
1415
lateinit var tvValue : TextView
@@ -20,8 +21,7 @@ class MainActivity : AppCompatActivity() ,SeekBar.OnSeekBarChangeListener{
2021
setContentView(R.layout.activity_main)
2122

2223

23-
myStepBarView = findViewById(R.id.my_stepBarView4)
24-
myStepBarView.allowSelectStep = object : StepBarView.AllowSelectStep{
24+
my_stepBarView4.allowSelectStep = object : StepBarView.AllowSelectStep{
2525
override fun allowSelectStep(step: Int) = step != 2
2626
}
2727

@@ -31,6 +31,27 @@ class MainActivity : AppCompatActivity() ,SeekBar.OnSeekBarChangeListener{
3131

3232

3333
initSpinner()
34+
initStepNames()
35+
}
36+
37+
private fun initStepNames() {
38+
my_stepBarView1.stepsTitleSetter = object : StepsTitleSetter {
39+
override fun getStepTitle(step: Int): String {
40+
return when (step) {
41+
1 -> "Fist"
42+
2 -> "Second"
43+
3 -> "Third"
44+
4 -> "Fourth"
45+
5 -> "Fifth"
46+
6 -> "Sixth"
47+
7 -> "Seventh"
48+
8 -> "Eighth"
49+
9 -> "Ninth"
50+
10 -> "Tenth"
51+
else -> "Non"
52+
}
53+
}
54+
}
3455
}
3556

3657
private fun initSpinner() {
@@ -59,7 +80,7 @@ class MainActivity : AppCompatActivity() ,SeekBar.OnSeekBarChangeListener{
5980
}
6081

6182
spActions.setSelection(0)
62-
refreshValueText(myStepBarView.maxCount)
83+
refreshValueText(my_stepBarView4.maxCount)
6384
}
6485

6586

@@ -68,37 +89,37 @@ class MainActivity : AppCompatActivity() ,SeekBar.OnSeekBarChangeListener{
6889
when(actionsList[position]){
6990
"sbv_max_count" -> {
7091
sbAttrsValue.max = 20
71-
sbAttrsValue.progress = myStepBarView.maxCount-2
92+
sbAttrsValue.progress = my_stepBarView4.maxCount-2
7293
refreshValueText(sbAttrsValue.progress)
7394
}
7495
"sbv_steps_line_height" -> {
7596
sbAttrsValue.max = 20
76-
sbAttrsValue.progress = DpHandler.pxToDp(this@MainActivity, myStepBarView.stepsLineHeight.toInt())
97+
sbAttrsValue.progress = DpHandler.pxToDp(this@MainActivity, my_stepBarView4.stepsLineHeight.toInt())
7798
refreshValueText(sbAttrsValue.progress)
7899
}
79100
"sbv_steps_size" -> {
80101
sbAttrsValue.max = 80
81-
sbAttrsValue.progress = DpHandler.pxToDp(this@MainActivity, myStepBarView.stepsSize.toInt())
102+
sbAttrsValue.progress = DpHandler.pxToDp(this@MainActivity, my_stepBarView4.stepsSize.toInt())
82103
refreshValueText(sbAttrsValue.progress)
83104
}
84105
"sbv_steps_text_size" -> {
85106
sbAttrsValue.max = 25
86-
sbAttrsValue.progress = DpHandler.pxToSp(this@MainActivity, myStepBarView.stepsSize.toInt()).toInt()
107+
sbAttrsValue.progress = DpHandler.pxToSp(this@MainActivity, my_stepBarView4.stepsSize.toInt()).toInt()
87108
refreshValueText(sbAttrsValue.progress)
88109
}
89110
"sbv_steps_line_margin_left" -> {
90111
sbAttrsValue.max = 40
91-
sbAttrsValue.progress = DpHandler.pxToDp(this@MainActivity, myStepBarView.stepsLineMarginLeft.toInt())
112+
sbAttrsValue.progress = DpHandler.pxToDp(this@MainActivity, my_stepBarView4.stepsLineMarginLeft.toInt())
92113
refreshValueText(sbAttrsValue.progress)
93114
}
94115
"sbv_steps_line_margin_right" -> {
95116
sbAttrsValue.max = 100
96-
sbAttrsValue.progress = DpHandler.pxToDp(this@MainActivity, myStepBarView.stepsLineMarginRight.toInt())
117+
sbAttrsValue.progress = DpHandler.pxToDp(this@MainActivity, my_stepBarView4.stepsLineMarginRight.toInt())
97118
refreshValueText(sbAttrsValue.progress)
98119
}
99120
"sbv_allow_touch_step_to" -> {
100-
sbAttrsValue.max = myStepBarView.maxCount
101-
sbAttrsValue.progress = myStepBarView.allowTouchStepTo
121+
sbAttrsValue.max = my_stepBarView4.maxCount
122+
sbAttrsValue.progress = my_stepBarView4.allowTouchStepTo
102123
refreshValueText(sbAttrsValue.progress)
103124
}
104125
}
@@ -135,25 +156,25 @@ class MainActivity : AppCompatActivity() ,SeekBar.OnSeekBarChangeListener{
135156
private fun refreshProgress(progress: Int) {
136157
when(actionsList[spActions.selectedItemPosition]){
137158
"sbv_max_count" -> {
138-
myStepBarView.maxCount= progress+2
159+
my_stepBarView4.maxCount= progress+2
139160
}
140161
"sbv_steps_line_height" -> {
141-
myStepBarView.stepsLineHeight = DpHandler.dpToPx(this@MainActivity,progress).toFloat()
162+
my_stepBarView4.stepsLineHeight = DpHandler.dpToPx(this@MainActivity,progress).toFloat()
142163
}
143164
"sbv_steps_size" -> {
144-
myStepBarView.stepsSize = DpHandler.dpToPx(this@MainActivity,progress).toFloat()
165+
my_stepBarView4.stepsSize = DpHandler.dpToPx(this@MainActivity,progress).toFloat()
145166
}
146167
"sbv_steps_text_size" -> {
147-
myStepBarView.stepsTextSize = DpHandler.spToPx(this,progress.toFloat())
168+
my_stepBarView4.stepsTextSize = DpHandler.spToPx(this,progress.toFloat())
148169
}
149170
"sbv_steps_line_margin_left" -> {
150-
myStepBarView.stepsLineMarginLeft = DpHandler.dpToPx(this@MainActivity,progress).toFloat()
171+
my_stepBarView4.stepsLineMarginLeft = DpHandler.dpToPx(this@MainActivity,progress).toFloat()
151172
}
152173
"sbv_steps_line_margin_right" -> {
153-
myStepBarView.stepsLineMarginRight = DpHandler.dpToPx(this@MainActivity,progress).toFloat()
174+
my_stepBarView4.stepsLineMarginRight = DpHandler.dpToPx(this@MainActivity,progress).toFloat()
154175
}
155176
"sbv_allow_touch_step_to" -> {
156-
myStepBarView.allowTouchStepTo = progress
177+
my_stepBarView4.allowTouchStepTo = progress
157178
}
158179
}
159180
}

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
android:paddingRight="24dp"
3131
app:sbv_show_step_stroke="true"
3232
app:sbv_is_rtl="true"
33+
app:sbv_show_step_name="true"
3334
/>
3435

3536
<ir.neo.stepbarview.StepBarView

stepbarview/src/main/java/ir/neo/stepbarview/StepBarView.kt

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int =
2323

2424
companion object {
2525
private val IS_DEBUG = false
26+
27+
// This is a fixed distance to add space between the circle and the step name
28+
private val NAME_STEP_SEPARATION_PX = 10
2629
}
2730

2831
private var stepsPaint : Paint
@@ -137,6 +140,12 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int =
137140
invalidate()
138141
}
139142

143+
var showStepName: Boolean = true
144+
set(value) {
145+
field = value
146+
invalidate()
147+
}
148+
140149
var stepsStrokeSize : Float = 0f
141150
set(value) {
142151
field = value
@@ -178,6 +187,12 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int =
178187
override fun allowSelectStep(step: Int) = true
179188
}
180189

190+
var stepsTitleSetter = object : StepsTitleSetter {
191+
192+
override fun getStepTitle(step: Int) = "Step ${step + 1}"
193+
194+
}
195+
181196
init {
182197
maxCount = 8
183198
reachedStep = 1
@@ -200,6 +215,7 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int =
200215
allowTouchStepTo = maxCount
201216

202217
showStepIndex = true
218+
showStepName = false
203219

204220
stepsStrokeSize = DpHandler.dpToPx(context,2).toFloat()
205221

@@ -235,6 +251,8 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int =
235251

236252
showStepIndex = a.getBoolean(R.styleable.StepBarView_sbv_show_step_index, showStepIndex)
237253

254+
showStepName = a.getBoolean(R.styleable.StepBarView_sbv_show_step_name, showStepName)
255+
238256
stepsStrokeSize = a.getDimension(R.styleable.StepBarView_sbv_steps_stroke_size,stepsStrokeSize)
239257

240258
stepsStrokeReachedColor = a.getColor(R.styleable.StepBarView_sbv_steps_stroke_reached_color,stepsStrokeReachedColor)
@@ -264,6 +282,13 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int =
264282
style = Paint.Style.FILL
265283
textAlign = Paint.Align.CENTER
266284
}
285+
286+
//If showStepName is true we get the text height with a sample text
287+
//to be considered when onMeasure() is called
288+
if (showStepName) {
289+
stepsTextPaint.color = stepsTextColor
290+
stepsTextPaint.textSize = stepsTextSize
291+
}
267292
}
268293

269294

@@ -285,9 +310,16 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int =
285310
}
286311

287312

288-
private fun calculateDesireHeight() = rawHeiht + paddingTop + paddingBottom
313+
private fun calculateDesireHeight() = rawHeiht + paddingTop + paddingBottom + titleTextHeight()
289314

315+
//To include the steps name height when onMeasure is called
316+
//if showStepName is false, this is 0
317+
private fun titleTextHeight() : Int {
318+
if (!showStepName) return 0
290319

320+
stepsTextPaint.getTextBounds("sample", 0, "sample".length, tmpRect)
321+
return tmpRect.height() + NAME_STEP_SEPARATION_PX * 2
322+
}
291323
private fun getHorizontalCirclesPosition() : FloatArray {
292324
val stepsHorizontalPositions = FloatArray(maxCount)
293325
val linesSize = linesWidth
@@ -384,6 +416,17 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int =
384416
(yPos + (tmpRect.height() / 2)),
385417
stepsTextPaint)
386418
}
419+
420+
//Draw Steps Names
421+
if(showStepName) {
422+
val name = stepsTitleSetter.getStepTitle(i + 1);
423+
stepsTextPaint.getTextBounds(name, 0, name.length, tmpRect)
424+
canvas?.drawText(
425+
name,
426+
xPos,
427+
yPos*2 + tmpRect.height() + NAME_STEP_SEPARATION_PX,
428+
stepsTextPaint)
429+
}
387430
}
388431

389432
}
@@ -584,4 +627,7 @@ constructor(mContext : Context, attrs: AttributeSet? = null, defStyleAttr: Int =
584627
fun allowSelectStep(step: Int) : Boolean
585628
}
586629

630+
interface StepsTitleSetter{
631+
fun getStepTitle(step: Int) : String
632+
}
587633
}

stepbarview/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<attr name="sbv_steps_line_margin_right" format="dimension"/>
1616
<attr name="sbv_allow_touch_step_to" format="integer"/>
1717
<attr name="sbv_show_step_index" format="boolean"/>
18+
<attr name="sbv_show_step_name" format="boolean"/>
1819
<attr name="sbv_steps_stroke_size" format="dimension"/>
1920
<attr name="sbv_steps_stroke_reached_color" format="dimension"/>
2021
<attr name="sbv_steps_stroke_unreached_color" format="dimension"/>

0 commit comments

Comments
 (0)