info@androidpaper.co.in

ProgressBar in Kotlin

Creating a progress bar in Kotlin can be achieved using various approaches, but one common way is by using the ProgressBar widget from the Android framework, assuming you're building an Android application. Below is a step-by-step guide to creating a basic progress bar in Kotlin:

Create an XML layout for your activity or fragment containing the ProgressBar: Assuming you have an activity layout called activity_main.xml, you can add the ProgressBar widget like this:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:indeterminate="true" />
<!-- adding textview which will show the progress -->
    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/progress_Bar"
        android:layout_centerHorizontal="true" />
  
     <!-- adding button to start the progress -->
    <Button
        android:id="@+id/show_button"
        android:layout_width="191dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text_view"
        android:layout_marginLeft="70dp"
        android:layout_marginTop="20dp"
        android:text="Progress Bar"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="70dp" />
</LinearLayout>

In your Kotlin code, reference the ProgressBar and handle its visibility:

import androidx.appcompat.app.AppCompatActivity
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?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
         
        // finding progressbar by its id
        progressBar = findViewById<ProgressBar>(R.id.progress_Bar) as ProgressBar
       
        // finding textview by its id
        txtView = findViewById<TextView>(R.id.text_view)
         
        // finding button by its id
        val btn = findViewById<Button>(R.id.show_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
                    handler.post(Runnable {
                        progressBar.progress = i                       
                        // setting current progress to the textview
                        txtView!!.text = i.toString() + "/" + progressBar.max
                    })
                    try {
                        Thread.sleep(100)
                    } catch (e: InterruptedException) {
                        e.printStackTrace()
                    }
                }
                 
                // 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
               
            }).start()
        }
    }
}