Programming

How To Create Loading Alert Dialog Android Studio

Welcome to Android Series. Today we are going to talk about how to implement a simple Loading Alert Dialog box using progress bar and a simple textView. Here I use Android Studio IDE 4.1. Firstly, open your android project and then follow these steps.


Step 01 : Create custom layout resource file

Right click on the res folder and choose New > Android Resource File. Then set file name as “custom_dialog_loading”.

Loading Alert Dialog Figure 01
Figure 01

Then enter the below code in custom_dialog_loading.xml file.


<?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:padding="50dp"
    android:layout_height="wrap_content">

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Loading..."
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/progressBar" />
</androidx.constraintlayout.widget.ConstraintLayout>

Now you can see design view like below.

Loading Alert Dialog Figure 02
Figure 02

Step 02: Create a java class

Right click on the java com folder and choose New > Java class. Then set file name as “loadingDialog” and create java class.

Loading Alert Dialog Figure 03
Figure 03

Then enter the below code in loadingDialog.java file.

package com.example.loadingdialogbox;

import android.app.Activity;
import android.view.LayoutInflater;

import androidx.appcompat.app.AlertDialog;

class loadingDialog {

    private Activity activity;
    private AlertDialog alertDialog;

    loadingDialog(Activity myactivity)
    {
        activity= myactivity;
    }

    void startLoadingDialog()
    {
        androidx.appcompat.app.AlertDialog.Builder builder = new androidx.appcompat.app.AlertDialog.Builder(activity);
        LayoutInflater inflater = activity.getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.custom_dialog_loading,null));
        builder.setCancelable(false);


        alertDialog = builder.create();
        alertDialog.show();
    }


    void dismisDialog()
    {
        alertDialog.dismiss();
    }
}

Then Loading Alert Dialog box construction is complete. Now, we can call Loading Alert Dialog from anywhere. Moreover, We can use this loading Dialog in two ways. Let us see how it use.

Firstly, let’s see how we call Loading Alert Dialog in Handler. After pressing the button or starting a corresponding life cycle ( Eg:- OnResume, OnStart), Using Handler we can schedule Loading Alert Dialog at a specific time (Eg: 5 seconds). After that time the Loading Alert Dialog automatically discarded (dismissed).


Step 03: Call to Loading Dialog

Open your Main Activity or relevant activity you required to call loading dialog box. Then, type the following method as your requirement. If you are want to run loading dialog box when button click, call this method in the onclick method of your button.

Otherwise, if you want to run loading dialog box when you start an activity you can call this method in the “onResume” function.

 public void openLoadingDialog()
    {
        loadingDialog loadingDialog = new loadingDialog(MainActivity.this);
        loadingDialog.startLoadingDialog();


        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadingDialog.dismisDialog();
            }
        },5000); //You can change this time as you wish
    }

Here as a example I want to show how open loading dialog box when start an activity. Now you can see full code of my Main Activity.

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;

public class MainActivity extends AppCompatActivity {

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


    @Override
    protected void onResume() {
        super.onResume();
        loadingDialog loadingDialog = new loadingDialog(MainActivity.this);
        loadingDialog.startLoadingDialog();


        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadingDialog.dismisDialog();
            }
        },5000);
    }
    
}


Loading Alert Dialog figure 04

Finally, You can get Loading Alert Dialog Box like this.


In the previous example, we talk about, how to dismiss loading dialog after a certain time. Now you know after creating one loading dialog box you can call it anywhere like this.

loadingDialog loadingDialog = new loadingDialog(MainActivity.this);
loadingDialog.startLoadingDialog();

After call loading dialog box you can discard it anytime calling below function.

loadingDialog.dismisDialog();

As an example, when you deal with databases, if you want to run loading dialog during data transmission, You can call loading dialog after pressing your submit button or something. Then you can call to “dismisDialog()” function in response method.

I think now you have an idea about how to create Loading Alert Dialog Box in Android Studio and how it uses. In this lesson, If you have any problem with this lesson comment it. Let’s meet in the next lesson. And you can follow our Web Design Tutorial HERE.

Happy Coding!

Nirodha Wickramarathna

Nirodha Wickramarathna is a dynamic full-stack software engineer, adept at crafting innovative digital solutions. With expertise in both front-end and back-end technologies, Nirodha brings a holistic approach to software development. Passionate about staying ahead in the tech world, Nirodha shares insights, coding tips, and experiences on this blog. Join the journey of coding excellence with Nirodha!

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights