How to Create Progress Dialog Using ProgressBar and AlertDialog in Android?
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.
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
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:id = "@+id/relative_layout_1" tools:context = ".MainActivity" > < Button android:id = "@+id/button_1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "click" android:layout_centerInParent = "true" /> </ RelativeLayout > |
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.
Kotlin
package org.geeksforgeeks.progressdialog import android.annotation.SuppressLint import android.app.AlertDialog import android.graphics.Color 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 import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Declaring and initializing // the Button from the layout file val mButton = findViewById<Button>(R.id.button_1) // When Button is clicked, // a function is called mButton.setOnClickListener { setProgressDialog() } } // Function to display ProgressBar // inside AlertDialog @SuppressLint ( "SetTextI18n" ) 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( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT ) 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( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT ) llParam.gravity = Gravity.CENTER // Creating a TextView inside the layout val tvText = TextView( this ) tvText.text = "Loading ..." tvText.setTextColor(Color.parseColor( "#000000" )) tvText.textSize = 20f tvText.layoutParams = llParam ll.addView(progressBar) ll.addView(tvText) // Setting the AlertDialog Builder view // as the Linear layout created above val builder: AlertDialog.Builder = AlertDialog.Builder( this ) builder.setCancelable( true ) builder.setView(ll) // Displaying the dialog val dialog: AlertDialog = builder.create() dialog.show() val window: Window? = dialog.window if (window != null ) { val layoutParams = WindowManager.LayoutParams() layoutParams.copyFrom(dialog.window?.attributes) 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) } } } |
Output:
You can see that the AlertDialog displays with ProgressBar and a TextView.
Please Login to comment...