0% found this document useful (0 votes)
107 views2 pages

Display Alert Message in Android

This document provides a code sample to display an alert message in an Android application. The code creates an AlertDialog with positive and negative buttons and a title and message. It defines an OnClickListener for the buttons that contains a switch statement to perform different actions depending on which button is clicked. The AlertDialog is then built and shown to display the message and buttons in response to an event, such as a button click.

Uploaded by

Ajaypal Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views2 pages

Display Alert Message in Android

This document provides a code sample to display an alert message in an Android application. The code creates an AlertDialog with positive and negative buttons and a title and message. It defines an OnClickListener for the buttons that contains a switch statement to perform different actions depending on which button is clicked. The AlertDialog is then built and shown to display the message and buttons in response to an event, such as a button click.

Uploaded by

Ajaypal Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Display Alert message in Android

How to display Alert message in Android Application?


Here is a sample code of an application that explains how you can display a simple Alert
message in your Android application. The below code can be put on a button click event or
wherever you would like it to popup.

DialogInterface.OnClickListener dialogClickListener
= newDialogInterface.OnClickListener() { /*DialogInterface called while setting
the AlertDialog Buttons */
public void onClick(DialogInterface dialog, int which) {
//Here you can perform functions of Alert Dialog Buttons as shown
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked

break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Alert Dialog");// Set the Title of Alert Dialog
builder.setMessage("Are you sure?")
.setPositiveButton("Yes",dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();/* Setting the Alert
message with buttons Yes and No */

//You can also set the icon of Alert Dialog using this code
builder.setIcon(R.drawable.icon);

You might also like