Progress Dialog | Android Studio | Kotlin

Simple Progress Dialog | Android Studio | Java 

What are Progress Bars? 

The Progress bars are used to show the progress of some ongoing task for example if you are downloading some file(s) from the internet you can display the progress bar while the file is being downloaded. Another example could be that when you are creating an account you can show progress bar while the account is being created. 
In Android, there is a class named ProgressDialog that is a type of dialog and allows you to show progress while doing some task(s). 
>>Watch For Java

Step 1: Create a new project or open an existing project

Step 2: 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:text="Progress Dialog"
        android:layout_gravity="center_horizontal"
        android:textColor="#000" />

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

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

</LinearLayout>

MainActivity.kt
package com.blogspot.atifsoftwares.myapplication

import android.app.ProgressDialog
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

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

        //init progressDialog
        val 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() }
    }
    
}

Output

Comments

Popular posts from this blog

Picture In Picture | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Kotlin

Add a Back Button to Action Bar Android Studio (Kotlin)