Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

How to Create Progress Dialog Using ProgressBar and AlertDialog in Android?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In Android, a Progress bar is a UI element used to display the progress of any running task or an operation. An Alert Dialog is a type of alert message displayed over the screen that lets users choose between options to respond to the message of the alert. Both of these elements are different from each other in nature and in serving a purpose. However, both of these can be brought together to create a Progress Dialog that would display the progress inside an Alert Dialog.

Progress Dialog in Android

So in this article, we will show you how you could implement a Progress bar inside an Alert Dialog to create a Progress Dialog in Android. Follow the below steps once the IDE is ready.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add a Button as shown below. This Button would be programmed in the main code to display Alert Dialog.


<?xml version="1.0" encoding="utf-8"?>
        android:layout_centerInParent="true" />

Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.


package org.geeksforgeeks.progressdialog
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import android.widget.Button
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.TextView
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Declaring and initializing 
        // the Button from the layout file
        val mButton = findViewById<Button>(
        // When Button is clicked,
        // a function is called
        mButton.setOnClickListener {
    // Function to display ProgressBar
    // inside AlertDialog
    fun setProgressDialog() {
        // Creating a Linear Layout
        val llPadding = 30
        val ll = LinearLayout(this)
        ll.orientation = LinearLayout.HORIZONTAL
        ll.setPadding(llPadding, llPadding, llPadding, llPadding)
        ll.gravity = Gravity.CENTER
        var llParam = LinearLayout.LayoutParams(
        llParam.gravity = Gravity.CENTER
        ll.layoutParams = llParam
        // Creating a ProgressBar inside the layout
        val progressBar = ProgressBar(this)
        progressBar.isIndeterminate = true
        progressBar.setPadding(0, 0, llPadding, 0)
        progressBar.layoutParams = llParam
        llParam = LinearLayout.LayoutParams(
        llParam.gravity = Gravity.CENTER
        // Creating a TextView inside the layout
        val tvText = TextView(this)
        tvText.text = "Loading ..."
        tvText.textSize = 20f
        tvText.layoutParams = llParam
        // Setting the AlertDialog Builder view 
        // as the Linear layout created above
        val builder: AlertDialog.Builder = AlertDialog.Builder(this)
        // Displaying the dialog
        val dialog: AlertDialog = builder.create()
        val window: Window? = dialog.window
        if (window != null) {
            val layoutParams = WindowManager.LayoutParams()
            layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT
            layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT
            dialog.window?.attributes = layoutParams
            // Disabling screen touch to avoid exiting the Dialog
            window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)


You can see that the AlertDialog displays with ProgressBar and a TextView.

My Personal Notes arrow_drop_up
Last Updated : 14 Feb, 2022
Like Article
Save Article
Similar Reads