Progress Dialog in Android using kotlin
Hello developer in this tutorial, I am going to show tell you how to use the android progress dialog in your android application using kotlin language in android studio. The progress dialog UI element is used to show progress on the android app. a progress dialog is used to show the progress of a task. mainly we use progress dialog when downloading or uploading any file. it would hold a progress bar inside an alert dialog.
in this tutorial, I will tell you how you can use the progressdialog in your android app using kotlin language. follow the step by step guide for how to add progress dialog features on your android app using android studio.
Progress Dialog in Android using Kotlin
quick look into the below code to add progressdialog features on your android app using android studio.
1 2 3 |
val progressDialog = ProgressDialog(this) progressDialog.setTitle("Please Wait") progressDialog.setMessage("Loading ...") |
Step 1 – Open android studio and create a new project in android studio. click on file -> New -> New Project
Step 2 -Select an empty activity and click next button. Now fill all required details and select kotlin from drop-down menu and then click the next button to create a new kotlin project.
You may know also:
WebView in an Android App using Kotlin language
Enable Webview Zoom Controls Using Kotlin In Android
Android Calculator in Kotlin Using Android Studio
Step 3 – Now Open res -> layout -> activity_main.xml file and add below code.the XML contains a button that invokes a progress dialog on click.
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?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:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/Progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Show Progress" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.497" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> |
Step 5- Let’s write the backend code to display the progressdialog box in your android device.
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import android.app.ProgressDialog import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import kotlinx.android.synthetic.main.activity_progress__dialog.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val progressDialog = ProgressDialog(this) progressDialog.setTitle("Please Wait") progressDialog.setMessage("Loading ...") Progress.setOnClickListener { progressDialog.show() } } } |
Below is the output screenshot when you will run the android app in android emulator or your real android device.