Skip to content

Commit

Permalink
added xml attributes and rimWidth customisablity
Browse files Browse the repository at this point in the history
  • Loading branch information
2hamed committed Jun 26, 2018
1 parent 5cbc947 commit 8a5ecad
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
progressBar.progress = 5
progressBar.textColor = Color.GREEN
progressBar.rimColor = Color.MAGENTA

updateBtn.setOnClickListener {
progressBar.progress = progressValue.text.toString().toInt()
Expand Down
12 changes: 9 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@

<com.hmomeni.progresscircula.ProgressCircula
android:id="@+id/progressBar"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="150dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:padding="6dp"
app:indeterminate="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent"
app:progress="40"
app:rimColor="@color/colorAccent"
app:rimWidth="3dp"
app:showProgress="true"
app:textColor="#00FF00" />

<EditText
android:id="@+id/progressValue"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,24 @@ import android.view.View

class ProgressCircula(context: Context, attributeSet: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attributeSet, defStyleAttr) {

constructor(context: Context, attributeSet: AttributeSet? = null) : this(context, attributeSet, 0)
constructor(context: Context, attributeSet: AttributeSet? = null) : this(context, attributeSet, 0) {
val a = context.theme.obtainStyledAttributes(
attributeSet,
R.styleable.ProgressCircula,
0, 0)

try {
progress = a.getInteger(R.styleable.ProgressCircula_progress, progress)
showProgressText = a.getBoolean(R.styleable.ProgressCircula_showProgress, showProgressText)
indeterminate = a.getBoolean(R.styleable.ProgressCircula_indeterminate, indeterminate)
rimColor = a.getInteger(R.styleable.ProgressCircula_rimColor, rimColor)
rimWidth = a.getDimension(R.styleable.ProgressCircula_rimWidth, rimWidth)
textColor = a.getInteger(R.styleable.ProgressCircula_textColor, textColor)
} finally {
a.recycle()
}

}

private val oval = RectF()
private val textBounds = Rect()
Expand All @@ -29,6 +46,7 @@ class ProgressCircula(context: Context, attributeSet: AttributeSet? = null, defS
set(value) {
field = value
if (value) {
showProgressText = false
isRotating = true
postInvalidate()
}
Expand All @@ -50,9 +68,15 @@ class ProgressCircula(context: Context, attributeSet: AttributeSet? = null, defS
outerRim.color = value
}

var rimWidth = dpToPx(3).toFloat()
set(value) {
field = value
outerRim.strokeWidth = value
}

private val outerRim = Paint().apply {
color = rimColor
strokeWidth = dpToPx(3).toFloat()
strokeWidth = rimWidth
style = Paint.Style.STROKE
strokeCap = Paint.Cap.ROUND
}
Expand Down
11 changes: 11 additions & 0 deletions progresscircula/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ProgressCircula">
<attr name="showProgress" format="boolean" />
<attr name="indeterminate" format="boolean" />
<attr name="textColor" format="color" />
<attr name="rimColor" format="color" />
<attr name="progress" format="integer" />
<attr name="rimWidth" format="dimension" />
</declare-styleable>
</resources>

0 comments on commit 8a5ecad

Please sign in to comment.