Skip to content

Commit 0db45a5

Browse files
committed
Add support for customizable indicator size and margin
1 parent 453fdb9 commit 0db45a5

File tree

3 files changed

+49
-44
lines changed

3 files changed

+49
-44
lines changed

overflow-library/src/main/java/cz/intik/overflowindicator/OverflowPagerIndicator.kt

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import android.content.Context
44
import android.graphics.drawable.Drawable
55
import android.graphics.drawable.GradientDrawable
66
import android.util.AttributeSet
7-
import android.util.TypedValue
87
import android.view.View
98
import android.widget.LinearLayout
109
import androidx.core.content.ContextCompat
@@ -29,16 +28,22 @@ import kotlin.math.max
2928
*/
3029
class OverflowPagerIndicator(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {
3130

31+
var indicatorSize: Int = -1
32+
private set
33+
34+
var indicatorMargin: Int = -1
35+
private set
36+
37+
var dotStrokeColor: Int = -1
38+
private set
39+
40+
var dotFillColor: Int = -1
41+
private set
42+
43+
private val dataObserver: OverflowDataObserver
3244
private var indicatorCount: Int = 0
3345
private var lastSelected: Int = 0
34-
private val indicatorSize: Int
35-
private val indicatorMargin: Int
36-
3746
private var recyclerView: RecyclerView? = null
38-
private val dataObserver: OverflowDataObserver
39-
40-
private var dotStrokeColor: Int = 0
41-
private var dotFillColor: Int = 0
4247

4348
private val dotDrawable: Drawable
4449
get() = GradientDrawable().apply {
@@ -50,43 +55,38 @@ class OverflowPagerIndicator(context: Context, attrs: AttributeSet) : LinearLayo
5055
init {
5156
initAttrs(context, attrs)
5257

53-
val dm = resources.displayMetrics
54-
indicatorSize = TypedValue.applyDimension(
55-
TypedValue.COMPLEX_UNIT_DIP, INDICATOR_SIZE_DIP.toFloat(), dm
56-
).toInt()
57-
58-
indicatorMargin = TypedValue.applyDimension(
59-
TypedValue.COMPLEX_UNIT_DIP, INDICATOR_MARGIN_DIP.toFloat(), dm
60-
).toInt()
61-
6258
dataObserver = OverflowDataObserver(this)
6359
}
6460

6561
private fun initAttrs(context: Context, attrs: AttributeSet?) {
6662
dotFillColor = ContextCompat.getColor(context, R.color.dot_fill)
6763
dotStrokeColor = ContextCompat.getColor(context, R.color.dot_stroke)
64+
indicatorSize = context.resources.getDimensionPixelSize(R.dimen.indicator_size)
65+
indicatorMargin = context.resources.getDimensionPixelSize(R.dimen.indicator_margin)
6866

69-
if (attrs != null) {
70-
val attributeArray =
71-
context.obtainStyledAttributes(attrs, R.styleable.OverflowPagerIndicator)
67+
if (attrs == null) return
7268

73-
if (attributeArray != null) {
74-
if (attributeArray.hasValue(R.styleable.OverflowPagerIndicator_dotFillColor)) {
75-
dotFillColor = attributeArray.getColor(
76-
R.styleable.OverflowPagerIndicator_dotFillColor,
77-
dotFillColor
78-
)
79-
}
69+
val attributeArray =
70+
context.obtainStyledAttributes(attrs, R.styleable.OverflowPagerIndicator)
8071

81-
if (attributeArray.hasValue(R.styleable.OverflowPagerIndicator_dotStrokeColor)) {
82-
dotStrokeColor = attributeArray.getColor(
83-
R.styleable.OverflowPagerIndicator_dotStrokeColor,
84-
dotStrokeColor
85-
)
86-
}
87-
88-
attributeArray.recycle()
89-
}
72+
try {
73+
dotFillColor = attributeArray
74+
.getColor(R.styleable.OverflowPagerIndicator_dotFillColor, dotFillColor)
75+
76+
dotStrokeColor = attributeArray
77+
.getColor(R.styleable.OverflowPagerIndicator_dotStrokeColor, dotStrokeColor)
78+
79+
indicatorSize = attributeArray
80+
.getDimensionPixelSize(
81+
R.styleable.OverflowPagerIndicator_indicatorSize, indicatorSize
82+
)
83+
84+
indicatorMargin = attributeArray
85+
.getDimensionPixelSize(
86+
R.styleable.OverflowPagerIndicator_indicatorMargin, indicatorMargin
87+
)
88+
} finally {
89+
attributeArray.recycle()
9090
}
9191
}
9292

@@ -237,16 +237,14 @@ class OverflowPagerIndicator(context: Context, attrs: AttributeSet) : LinearLayo
237237
}
238238

239239
companion object {
240-
private const val MAX_INDICATORS = 9
241-
private const val INDICATOR_SIZE_DIP = 12
242-
private const val INDICATOR_MARGIN_DIP = 2
240+
const val MAX_INDICATORS = 9
243241

244242
// State also represents indicator scale factor
245-
private const val STATE_GONE = 0f
246-
private const val STATE_SMALLEST = 0.2f
247-
private const val STATE_SMALL = 0.4f
248-
private const val STATE_NORMAL = 0.6f
249-
private const val STATE_SELECTED = 1.0f
243+
const val STATE_GONE = 0f
244+
const val STATE_SMALLEST = 0.2f
245+
const val STATE_SMALL = 0.4f
246+
const val STATE_NORMAL = 0.6f
247+
const val STATE_SELECTED = 1.0f
250248
}
251249

252250
}

overflow-library/src/main/res/values/attrs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
<declare-styleable name="OverflowPagerIndicator">
44
<attr name="dotFillColor" format="color"/>
55
<attr name="dotStrokeColor" format="color"/>
6+
<attr name="indicatorSize" format="dimension" />
7+
<attr name="indicatorMargin" format="dimension" />
68
</declare-styleable>
79
</resources>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<dimen name="indicator_size">12dp</dimen>
4+
<dimen name="indicator_margin">2dp</dimen>
5+
</resources>

0 commit comments

Comments
 (0)