Android Alert, Confirm and Prompt dialog


A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed.

3 most used dialog window are:

  • Alert dialog : We use Alert dialog to give some message to user. For example, suppose a user has started download process of a file from server, once download completed then we can use Alert dialog to inform him that Download completed successfully
  • Confirm dialog : We use Confirm dialog to get user's permission before doing some critical task. For example, suppose a user has clicked on a Delete button that will delete all the app data from the device, in that case we can use Confirm dialog to get user's confirmation.
  • Prompt dialog : We can use Prompt dialog if app requires some input from user at run time.

Android Alert, Confirm, Prompt dialog Android Alert, Confirm, Prompt dialog

Android Alert, Confirm, Prompt dialog Android Alert, Confirm, Prompt dialog


Building alert dialog


Here is code to create an Alert dialog:


Building confirm dialog


Here is code to create a confirm dialog:


Building prompt dialog


Here is code to create a prompt dialog:


Example


In this example, I've demonstrated Alert dialog, Confirm dialog and Prompt dialog.

Step 1: Create a new project in Android Studio and name it Android dialogs.

Step 2:Open res -> drawable -> activity_main.xml and replace the code with code of activity_main.xml with the below given code.

activity_main.xml

Step 3: Open MainActivity.java and replace its code with below given code.

MainActivity.java

Step 4:Clean and run your project to see the result.

Share & Like

Tutorial topics

About ApnaTutorials.com

ApnaTutorials.com is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.