Progress Dialog | Android Studio | Java

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 Kotlin

Step 1: Create a new project OR Open your 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.java
package com.blogspot.atifsoftwares.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    //UI Views
    private Button showProgressBtn, hideProgressBtn;

    //progressDialog
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        //init progressDialog
        progressDialog = new 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(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressDialog.show();
            }
        });
        //handle click listener, hide progress dialog
        hideProgressBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressDialog.dismiss();
            }
        });

    }
}

Output
Progress Dialog | Android Studio | Java


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)