How to Implement Loading AlertDialog in Android?
AlertDialog is defined as the small window that shows a particular message to the user when the user performs or commits certain action. In this article, we are going to build a simple android application in which we learn how to implement a Loading AlertDialog that means whenever the user clicks on the submit button it shows a dialog with a loading ProgressBar and a message please wait and after a certain time it gets dismissed and shows a message done. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
In this step, we are going to design our activity_main.xml. These are the following things that we will add to our activity_main.xml
- An ImageView with the image of the GEEKSOFGEEKS symbol.
- Two EditTexts one for username and one for the password field
- A Button with the text SUBMIT.
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Step 3: Create a new layout file
In this step, we are going to create a new layout file for designing our dialog. Now create a new layout file to design the dialog that includes a loading ProgressBar and a text field with a text please wait. To create a new layout file please follow the following steps: Right-click on layout > new > Layout Resource file and name it loading and add the following code in it.
Below is the code for the loading.xml file:
Step 4: Create a new java file
In the fourth step create a new java file named Dialog to implement alert dialog and methods like startloadingdialog and dismissdialog. startloadingdialog function that will have an AlertDialog and on calling startloadingdialog function an alert loading dialog will appear on the user screen that gets dismisses after 4 seconds. dismissdialog function with the help of dialog.dismiss method that will dismiss the dialog. Below is the code for the Dialog.java file:
Step 5: Create a vector asset icon
In this step, we are going to create a vector asset icon that we will use in the 6th step to design a layout that appears on the screen after dismiss dialog function gets executed or invoked. Create a new vector asset for click icon using the following steps: Right-click on drawable > new > vector asset and search for check > select and finish
After changing color code to green:
Step 6: In this step, we are going to make a new activity that contains a vector asset click icon that we created in the previous step (step 5) and a TextView with text done this activity will be visible to the user after the dismissing of the dialog that happened in the interval of 4 seconds. Following is the xml code to implement vector asset click icon with the help of ImageView and a TextView with text done.
1. xml file
2. Java file
No need to change anything in the java class of this activity
Step 7: Working with the MainActivity.java file
Now in the final step, we are going to add the onclick listener on our submit button in our MainActivity.java file.
First, we will create an object of dialog class, and after finds the view of our submit button id we will implement set onclicklistener method on the submit button and invoke or implement the start loading function that we create in the dialog class and with the use of handler class we implement the time delay method for dismissdialog function and start new activity method using the intent that takes the user to finishedactivity after 4 seconds of delay time.
1. Home Activity
3. After 4 Seconds
Project Link: Click Here