Open In App
Related Articles

ProgressBar in Kotlin

Improve Article
Save Article
Like Article

Android ProgressBar is a user interface control that indicates the progress of an operation. For example, downloading a file, uploading a file on the internet we can see the progress bar to estimate the time remaining in operation. There are two modes of ProgressBar: 

  • Determinate ProgressBar
  • Indeterminate ProgressBar

Determinate ProgressBar 

In common, we use the Determinate progress mode in ProgressBar because it shows the quantity of progress that has occurred like the (%) percentage of the file downloaded, how much data was uploaded or downloaded on the internet, etc. If we have to use determinate we set the style of the progress bar as below: 


    android:layout_height="wrap_content" />

Indeterminate ProgressBar 

Here, we don’t get the idea of the progress of work means how much it has been completed or How long it will take to complete. 
We can use indeterminate progressBar like below by setting the indeterminate attribute as true. 



Different Attributes of ProgressBar Widgets

XML Attributes


android:idUsed to uniquely identify the control
android:minUsed to set minimum value
android:maxUsed to set the maximum value
android:progressUsed to set the default progress integer value between 0 and max.
android:minHeightUsed to set the height of the progress bar.
android:minWidthUsed to set the width of the progress bar.
android:backgroundUsed to set the background color for progress bar
android:indeterminateUsed to enable indeterminate progress mode.
android:paddingUsed to set the padding for left, right, top, or bottom of the progress bar.

Add ProgressBar Widget in activity_main.xml file


<?xml version="1.0" encoding="utf-8"?>
    <!-- adding progress bar -->
        android:indeterminate = "true"
        android:layout_marginStart="70dp" />
    <!-- adding textview which will show the progress -->
        android:layout_centerHorizontal="true" />
     <!-- adding button to start the progress -->
        android:text="Progress Bar"
        android:layout_marginStart="70dp" />

Access the ProgressBar Widget in MainActivity.kt file


import android.os.Bundle
import android.widget.Button
import android.widget.ProgressBar
import android.widget.TextView
import android.os.Handler
class MainActivity : AppCompatActivity() {
    private var progressBar: ProgressBar? = null
    private var i = 0
    private var txtView: TextView? = null
    private val handler = Handler()
    override fun onCreate(savedInstanceState: Bundle?) {
        // finding progressbar by its id
        progressBar = findViewById<ProgressBar>( as ProgressBar
        // finding textview by its id
        txtView = findViewById<TextView>(
        // finding button by its id
        val btn = findViewById<Button>(
        // handling click on button
        btn.setOnClickListener {
            // Before clicking the button the progress bar will invisible
            // so we have to change the visibility of the progress bar to visible
            // setting the progressbar visibility to visible
            progressBar.visibility = View.VISIBLE
            i = progressBar.progress
            Thread(Runnable {
                // this loop will run until the value of i becomes 99
                while (i < 100) {
                    i += 1
                    // Update the progress bar and display the current value
                        progressBar.progress = i                       
                        // setting current progress to the textview
                        txtView!!.text = i.toString() + "/" + progressBar.max
                    try {
                    } catch (e: InterruptedException) {
                // setting the visibility of the progressbar to invisible
                // or you can use View.GONE instead of invisible
                // View.GONE will remove the progressbar
                progressBar.visibility = View.INVISIBLE

AndroidManifest.xml file


<?xml version="1.0" encoding="utf-8"?>
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />


Last Updated : 01 Sep, 2021
Like Article
Save Article
Similar Reads
Related Tutorials