MainActivity.kt
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.widget.Button
import android.widget.ProgressBar
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.transition.TransitionManager
class MainActivity : Activity() {
var progressStatus = 0
var secondaryProgressStatus = 0
var handler = Handler(Looper.getMainLooper())
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Get the widgets reference from XML layout
val rootLayout = findViewById<ConstraintLayout>(R.id.rootLayout)
val button = findViewById<Button>(R.id.button)
val textView = findViewById<TextView>(R.id.textView)
val tvSecondary = findViewById<TextView>(R.id.tvSecondary)
val progressBar = findViewById<ProgressBar>(R.id.progressBar)
// button click listener
button.setOnClickListener {
button.isEnabled = false
TransitionManager.beginDelayedTransition(rootLayout)
progressBar.visibility = View.VISIBLE
// set up progress bar on initial stage
progressBar.progress = 0
progressStatus = 0
Thread {
while (progressStatus < 100) {
while (secondaryProgressStatus < 100) {
secondaryProgressStatus += 1
Thread.sleep(5)
handler.post {
progressBar.secondaryProgress =
secondaryProgressStatus
if (secondaryProgressStatus == 100) {
tvSecondary.text = "Secondary task finished."
secondaryProgressStatus = 0
progressStatus += 1
progressBar.progress = progressStatus
textView.text = "Task finished" +
" $progressStatus of 100"
if (progressStatus == 100) {
button.isEnabled = true
}
} else {
tvSecondary.text = "Secondary task " +
"$secondaryProgressStatus of 100"
}
}
}
}
}.start()
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp"
android:background="#DCDCDC">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:padding="12dp"
android:fontFamily="sans-serif"
android:textSize="20sp"
android:textStyle="bold"
tools:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:visibility="invisible"
android:secondaryProgressTint="#0018A8"
android:secondaryProgressTintMode="src_in"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/tvSecondary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif"
android:padding="12dp"
android:textSize="18sp"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBar"
tools:text="Secondary Task" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Start Task"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvSecondary" />
</androidx.constraintlayout.widget.ConstraintLayout>


- android kotlin - Toolbar center align title
- android kotlin - Change indeterminate progress bar color
- android kotlin - Change circular progress bar color
- android kotlin - Custom circular progress bar
- android kotlin - Vertical progress bar example
- android kotlin - Indeterminate ProgressBar example
- android kotlin - Horizontal ProgressBar programmatically
- android kotlin - Change horizontal ProgressBar color
- android kotlin - Horizontal ProgressBar example
- android kotlin - selectableItemBackground programmatically
- android kotlin - ListView delete item on click
- android kotlin - ListView disable item programmatically
- android kotlin - ListView item height programmatically
- android kotlin - ListView add header programmatically
- android kotlin - ListView OnItemClickListener example