Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Dynamic ProgressBar in Kotlin

  • Last Updated : 23 Feb, 2021

Android ProgressBar is user interface control that is used to show some kind of progress. For instance, loading of some page, downloading of some file or waiting for some event to complete.

In this article we will be discussing how to programmatically create a progress bar in Kotlin .

Firstly, we need to create a project in Android Studio. To do follow these steps:

  • Click on File, then New and then New Project and give name whatever you like
  • Then, select Kotlin language Support and click next button.
  • Select minimum SDK, whatever you need.
  • Select Empty activity and then click finish.

Modify activity_main.xml file

Second step is to design our layout page. Here, we will use the RelativeLayout to get the ProgressBar from the Kotlin file.

<?xml version="1.0" encoding="utf-8"?>
            android:text="Stop Loading"/>

Update the strings.xml file

    <string name="app_name">DynamicProgressBarInKotlin</string>

Create ProgressBar in MainActivity.kt file

Open app/src/main/java/yourPackageName/MainActivity.kt. In this file , we declare a variable progressBar to create the ProgressBar widget like this

       val progressBar = ProgressBar(this)
       //setting height and width of progressBar
       progressBar.layoutParams = LinearLayout.LayoutParams(

then add the widget in layout using this

val layout = findViewById(
     // Add ProgressBar to our layout

package com.geeksforgeeks.myfirstkotlinapp
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.RelativeLayout
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        val progressBar = ProgressBar(this)
        //setting height and width of progressBar
        progressBar.layoutParams = LinearLayout.LayoutParams(
        //accessing our relative layout where the progressBar will add up
        val layout = findViewById<RelativeLayout>(
        // Add ProgressBar to our layout
        //accessing the button which will handle the events,
        // whether to show progressBar or not
        val button = findViewById<Button>(
        //set a click listener to show/hide progressBar added in RelativeLayout.
        button?.setOnClickListener {
            val visibility = if (progressBar.visibility == View.GONE){
            progressBar.visibility = visibility
            //setting button text
            //if we click "stop loading" button, text of button will change
            // to "start loading.." and vice versa
            val btnText = if (progressBar.visibility == View.GONE)
                "START LOADING..."
                "STOP LOADING"
            button.text = btnText

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" />

Run as Emulator:

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!