Android Android Java

Android Alert Dialog Example with Positive, Negative and Natural Buttons in Android Studio4 min read

In this tutorial, we show you how to display an alert box in Android. See flowing Steps :

AlertDialog is a structure extended from Dialog that allows the user to see a popup image on the screen. We can show it as an information message to the user, or we can use the AlertDialog structure as an error message if we want.




Demo:

As you know Android AlertDialog is the subclass of Dialog class. So you can use Dialog class methods with AlertDialog.

MethodDescription
public AlertDialog.Builder setTitle(CharSequence)This method is used to set the title of AlertDialog.
public AlertDialog.Builder setMessage(CharSequence)This method is used to set the message for AlertDialog.
public AlertDialog.Builder setIcon(int)This method is used to set the icon over AlertDialog.

Android Alert Dialog Example

Step 1: Open the Android Studio IDE and Create a New Empty Activity. Name it similarly.

Step 1: Android Alert Dialog Example

Step 2: Simple layout file, display a button on screen.

File : res/layout/activity_main.xml

Step 3: Step 3: Create an Intent to be shown in case of positive response.

Create a New Activity into App folder by right click. New → Activity → Empty Activity. Name as PositiveAnswer.

Step 4: Let’s write the code to create and show the AlertDialog.

File: MainActivity.java

Leave a Comment