MainActivity.kt
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.content.res.ColorStateList
import android.graphics.BlendMode
import android.graphics.BlendModeColorFilter
import android.graphics.Color
import android.graphics.PorterDuff
import android.os.Build
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
import kotlin.random.Random
class MainActivity : Activity() {
var progressStatus = 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 progressBar = findViewById<ProgressBar>(R.id.progressBar)
val progressBar2 = findViewById<ProgressBar>(R.id.progressBar2)
val progressBar3 = findViewById<ProgressBar>(R.id.progressBar3)
val progressBar4 = findViewById<ProgressBar>(R.id.progressBar4)
// change indeterminate progressbar color (tint) programmatically
progressBar3.indeterminateTintList = ColorStateList
.valueOf(Color.RED)
progressBar3.indeterminateTintMode = PorterDuff.Mode.SRC_IN
// another way to change indeterminate progress bar color
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
progressBar4.indeterminateDrawable.colorFilter =
BlendModeColorFilter(Color.BLUE, BlendMode.SRC_IN)
}else {
progressBar4.indeterminateDrawable
.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN)
}
// button click listener
button.setOnClickListener {
button.isEnabled = false
TransitionManager.beginDelayedTransition(rootLayout)
progressBar.visibility = View.VISIBLE
progressBar2.visibility = View.VISIBLE
progressBar3.visibility = View.VISIBLE
progressBar4.visibility = View.VISIBLE
// set up progress bar on initial stage
progressBar.progress = 0
progressStatus = 0
// generate random number of files to download
val filesToDownload= Random.nextInt(200,500)
// set up max value for progress bar
progressBar.max = filesToDownload
Thread(Runnable {
while (progressStatus < filesToDownload){
// update progress status
progressStatus +=1
// sleep the thread for 50 milliseconds
Thread.sleep(50)
// update the progress bar
handler.post {
//progressBar.progress = progressStatus
// calculate the percentage
val percentage = ((progressStatus.toDouble()
/ filesToDownload) * 100).toInt()
// update the text view
textView.text = "Downloaded $progressStatus of " +
"$filesToDownload files ($percentage%)"
if (progressStatus == filesToDownload){
button.isEnabled = true
TransitionManager
.beginDelayedTransition(rootLayout)
progressBar.visibility = View.INVISIBLE
progressBar2.visibility = View.INVISIBLE
progressBar3.visibility = View.INVISIBLE
progressBar4.visibility = View.INVISIBLE
}
}
}
}).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:indeterminate="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:visibility="invisible"
android:indeterminate="true"
android:indeterminateTint="#00A86B"
android:indeterminateTintMode="src_in"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBar" />
<ProgressBar
android:id="@+id/progressBar3"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:visibility="invisible"
android:indeterminate="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBar2" />
<ProgressBar
android:id="@+id/progressBar4"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:visibility="invisible"
android:indeterminate="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBar3" />
<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/progressBar4" />
</androidx.constraintlayout.widget.ConstraintLayout>

- android kotlin - Toolbar center align title
- android kotlin - Secondary progress bar example
- 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 - AlertDialog EditText programmatically
- android kotlin - AlertDialog title align center programmatically
- android kotlin - AlertDialog title text color size bold programmatically
- android kotlin - Underline TextView text programmatically
- android kotlin - Add view to ConstraintLayout programmatically