Android Alert Dialog Example Codes with output

Using Dialogs is simplest one in Android Programming .This tutorial we will be discussing about how to use Alert Dialog with one button(yes and No) in Android Application.

In this post, I have created a simple android alert dialog box which shows a title of dialog, a text message and a button.

Android AlertDialog

Alert dialog box is used to show alerts to the users, get confirmation from the users. In order to make an alert dialog, we need to make an object of AlertDialogBuilder which is an inner class of AlertDialog.Its Syntax is given below

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
Android AlertDialog Components

Android AlertDialog Components

Title – This is optional
Content area – This displays the message to the user. It can be a string message, a list or custom layout
Action buttons – There should be no more than three action buttons in a dialog.There are three types of buttons they are Positive,Negative and Neutral

Also ReadAndroid Notification Example Using NotificationCompat

Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue.

Adding Buttons

Call the setPositiveButton(),setNegativeButton and setNeutralButton() methods:

Syntax :

alertDialogBuilder.setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener);
alertDialogBuilder.setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener);
alertDialogBuilder.setNeutralButton(CharSequence text, DialogInterface.OnClickListener listener);

Functions available to customise the alert dialog as given below

setIcon(Drawable icon) -T his method set the icon of the alert dialog box

setCancelable(boolean cancel able) – This method sets the property that the dialog can be cancelled or not

setTitle(CharSequence title) – This method set the title to be appear in the dialog

setMessage(CharSequence message) – This method sets the message to be displayed in the alert dialog

setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) -This method Sets the callback that will be called if the dialog is cancelled.

We should create an alert dialog by calling the create() method of the builder class. Its syntax is shown below.

AlertDialog alertDialog = alertDialogBuilder.create();;

Download Free Android Topics for Interview Preparation

Created a simple Android AlertDialog with Two Button
package com.example.alertdialog;

import android.os.Bundle;
import android.content.DialogInterface;
import android.view.Menu;

public class MainActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {

AlertDialog.Builder builder = new AlertDialog.Builder(this);

//Setting message manually and performing action on button click
builder.setMessage(“Do you want to close this application ?”)
.setPositiveButton(“Yes”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
.setNegativeButton(“No”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for ‘NO’ Button

//Creating dialog box
AlertDialog alert = builder.create();
//Setting the title manually

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu
getMenuInflater().inflate(, menu);
return true;


Output (3)