Open In App

Dynamic ProgressBar in Kotlin

Last Updated : 28 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Step by Step Implementation

Step 1: Create a new project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Step 2: Add ProgressBar Widget in activity_main.xml file

Navigate to app > res > layout > activity_main.xml and add a Progress bar and Button to show and hide the progress bar.

activity_main.xml:

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/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".MainActivity">

    <!--on below line we are creating a progress bar-->
    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:orientation="vertical"
        android:gravity="center"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed"/>

    <!--on below line we are creating a button-->
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="Show/Hide"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/layout" />

</androidx.constraintlayout.widget.ConstraintLayout>

Design UI:

dynamic-progress-bar-design-ui


Step 3: Create ProgressBar in MainActivity.kt file

Navigate to app > src > main > java > {package-name} > MainActivity.kt.

In this file, we will declare a variable progressBar to create the ProgressBar widget

val progressBar = ProgressBar(this)
progressBar.layoutParams = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)

then add the widget in layout

val layout: LinearLayout = findViewById(R.id.layout)
layout.addView(progressBar)

MainActivity.kt:

Kotlin
package org.geeksforgeeks.demo

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 androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    private var isProgressVisible = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // initializing our variables
        val showProgressBtn: Button = findViewById(R.id.button)
        val layout: LinearLayout = findViewById(R.id.layout)

        // creating a progress bar
        val progressBar = ProgressBar(this)
        progressBar.layoutParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)

        // Add ProgressBar to our layout
        layout.addView(progressBar)

        // set visibility off by default
        progressBar.visibility = View.GONE

        showProgressBtn.setOnClickListener {
            // checking if progress bar is already visible.
            if (isProgressVisible) {
                progressBar.visibility = View.GONE
                isProgressVisible = false
            } else {
                progressBar.visibility = View.VISIBLE
                isProgressVisible = true
            }
        }
    }
}

Output:



Next Article

Similar Reads