Progress Dialog - Android Studio - Kotlin

Progress Dialog in Android Studio using Kotlin

What are Progress Bars?

Progress bars are used to indicate that a task is running in the background, such as downloading files or creating a user account. They help improve user experience by providing visual feedback during long operations. 

In Android, ProgressDialog is a dialog-based component used to display loading or progress status while a task is being processed. This post explains how to implement a simple Progress Dialog in Android Studio using Kotlin.

Code:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:padding="20dp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp"
        android:text="Progress Dialog"
        android:textColor="#000"
        android:textSize="20sp" />

    <Button
        android:id="@+id/showProgressBtn"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="100dp"
        android:text="Show Progress" />

    <Button
        android:id="@+id/hideProgressBtn"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Hide Progress" />

</LinearLayout>

MainActivity.kt

package com.technifysoft.myapplication

import android.app.ProgressDialog
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity


class MainActivityKt : AppCompatActivity() {

    //UI Views
    private lateinit var showProgressBtn: Button
    private lateinit var hideProgressBtn: Button

    //progressDialog
    private lateinit var progressDialog: ProgressDialog

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

        //init UI Views
        showProgressBtn = findViewById(R.id.showProgressBtn)
        hideProgressBtn = findViewById(R.id.hideProgressBtn)

        //init progressDialog
        progressDialog = ProgressDialog(this)
        //set properties
        progressDialog.setTitle("Please Wait") //set title
        progressDialog.setMessage("Creating Account...") //set message
        progressDialog.setCanceledOnTouchOutside(false) //disable dismiss when touching outside of progress dialog

        //handle click listener, show progress dialog
        showProgressBtn.setOnClickListener {
            progressDialog.show()
        }

        //handle click listener, hide progress dialog
        hideProgressBtn.setOnClickListener {
            progressDialog.dismiss()
        }

    }

}

Screenshots:



Comments

Popular posts from this blog

Manage External Storage Permission - Android Studio - Kotlin

Picture In Picture - Android Studio - Kotlin

SeekBar with Customization | Android Studio | Java