VOOZH about

URL: https://www.geeksforgeeks.org/android/how-to-create-an-alert-dialog-box-in-android/

⇱ How to Create an Alert Dialog Box in Android? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Create an Alert Dialog Box in Android?

Last Updated : 11 Jul, 2025

An Android Alert Dialog is a UI element that displays a warning or notification message and asks the user to respond with options such as Yes or No. Based on the user's response, appropriate actions are executed. Android Alert Dialog is built with the use of three fields: Title, Message area, and Action Button.

Alert Dialog code has three methods :

  • setTitle(): method for displaying the Alert Dialog box Title
  • setMessage(): method for displaying the message
  • setIcon(): method is used to set the icon on the Alert dialog box.

Then we add the two Buttons, setPositiveButton and setNegativeButton to our Alert Dialog Box as shown below.

Example:

👁 How-to-Create-an-Alert-Dialog-Box-in-Android_

Step By Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

The code for that has been given in both Java and Kotlin Programming Language for Android.

Step 2: Working with the XML Files

Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

activity_main.xml:

Layout:

👁 Layout_1


Step 3: Working with the MainActivity File

Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.

Output:

👁 alert-box


Comment
Article Tags:

Explore