Android Code Example: Alert Dialog
Android Code Example: Alert Dialog
Example
Android Source code example for Android development. Android
3 .. 2556
AlertDialog
Android Alert Dialog
A dialog is usually a small window that appears in front of the current Activity. The
underlying Activity loses focus and the dialog accepts all user interaction. Dialogs are
normally used for notifications that should interrupt the user and to perform short tasks that
directly relate to the application in progress (such as a progress bar or a login prompt).
The Dialog class is the base class for creating dialogs. However, you typically should not
instantiate a Dialog directly. Instead, you should use one of the following subclasses:
AlertDialog
A dialog that can manage zero, one, two, or three buttons, and/or a list of
selectable items that can include checkboxes or radio buttons. The AlertDialog is
capable of constructing most dialog user interfaces and is the suggested dialog
type.
ProgressDialog
Alert Dialog
-->
Alert Dialog MainActivity.java Source Code
package com.example.alertdialog;
import
import
import
import
import
import
import
import
import
android.os.Bundle;
android.app.Activity;
android.app.AlertDialog;
android.content.Context;
android.content.DialogInterface;
android.view.Menu;
android.view.View;
android.view.View.OnClickListener;
android.widget.Button;
//@Override
public void onClick(View arg0) {
AlertDialog.Builder alertDialogBuilder
= new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Your Title Here");
// set dialog message
alertDialogBuilder
.setMessage("Click Yes to Exit !!!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListe
ner() {
public void onClick(DialogInterface
dialog,int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListe
ner() {
public void onClick(DialogInterface
dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// Create Alert Dialog
AlertDialog alertDialog =
alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
}
android.annotation.TargetApi;
android.app.Activity;
android.app.AlertDialog;
android.app.Dialog;
android.app.ProgressDialog;
android.content.DialogInterface;
android.os.Bundle;
android.os.Handler;
android.os.Message;
android.view.LayoutInflater;
android.view.View;
android.view.View.OnClickListener;
android.widget.Button;
android.widget.Toast;
android.database.Cursor;
android.provider.ContactsContract;
@TargetApi(11)
public class AlertDialogsActivity extends Activity {
private static final int DIALOG_YES_NO_MESSAGE =
1;
private static final int DIALOG_YES_NO_LONG_MESSA
GE = 2;
private
private
private
private
static
static
static
static
final
final
final
final
int
int
int
int
DIALOG_LIST = 3;
DIALOG_PROGRESS = 4;
DIALOG_SINGLE_CHOICE = 5;
DIALOG_MULTIPLE_CHOICE =
6;
private static
private static
RSOR = 8;
private static
_MESSAGE = 9;
private static
_MESSAGE = 10;
private static
MESSAGE = 11;
}
});
/* Display an ultra long text message with yes/no
buttons and handle each message as well as the cancel
action */
Button twoButtons2UltraTitle = (Button)
findViewById(R.id.two_buttons2ultra);
twoButtons2UltraTitle.setOnClickListener(new OnClickList
ener() {
public void onClick(View v) {
showDialog(DIALOG_YES_NO_ULTRA_LONG_MES
SAGE);
}
});
/* Display a list of items */
Button selectButton = (Button)
findViewById(R.id.select_button);
selectButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG_LIST);
}
});
/* Display a custom progress bar */
Button progressButton = (Button)
findViewById(R.id.progress_button);
progressButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
showDialog(DIALOG_PROGRESS);
mProgress = 0;
mProgressDialog.setProgress(0);
mProgressHandler.sendEmptyMessage(0);
}
});
/* Display a radio button group */
Button radioButton = (Button)
findViewById(R.id.radio_button);
radioButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG_SINGLE_CHOICE);
}
});
/* Display a list of checkboxes */
Button checkBox = (Button)
findViewById(R.id.checkbox_button);
checkBox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG_MULTIPLE_CHOICE);
}
});
/* Display a list of checkboxes, backed by a cursor */
Button checkBox2 = (Button)
findViewById(R.id.checkbox_button2);
checkBox2.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
showDialog(DIALOG_MULTIPLE_CHOICE_CURSOR
);
}
});
/* Display a text entry dialog */
Button textEntry = (Button)
findViewById(R.id.text_entry_button);
textEntry.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG_TEXT_ENTRY);
}
});
/* Two points, in the traditional theme */
Button twoButtonsOldSchoolTitle = (Button)
findViewById(R.id.two_buttons_old_school);
twoButtonsOldSchoolTitle.setOnClickListener(new OnClick
Listener() {
public void onClick(View v) {
showDialog(DIALOG_YES_NO_OLD_SCHOOL_MES
SAGE);
}
});
/* Two points, in the light holographic theme */
Button twoButtonsHoloLightTitle = (Button)
findViewById(R.id.two_buttons_holo_light);
twoButtonsHoloLightTitle.setOnClickListener(new OnClick
Listener() {
mProgressDialog.setButton(DialogInterface.BUTTO
N_POSITIVE,
getText(R.string.alert_dialog_hide), new DialogInterface.O
nClickListener() {
public void onClick(DialogInterface
dialog, int whichButton) {
/* User clicked Yes so do some stuff */
}
});
mProgressDialog.setButton(DialogInterface.BUTTO
N_NEGATIVE,
getText(R.string.alert_dialog_cancel), new DialogInterface.
OnClickListener() {
public void onClick(DialogInterface
dialog, int whichButton) {
/* User clicked No so do some stuff */
}
});
return mProgressDialog;
case DIALOG_SINGLE_CHOICE:
return new AlertDialog.Builder(AlertDialogsActivit
y.this)
.setIconAttribute(android.R.attr.alertDialogIcon)
.setTitle(R.string.alert_dialog_single_choice)
.setSingleChoiceItems(R.array.select_dialog_items2,
0, newDialogInterface.OnClickListener() {
public void onClick(DialogInterface
dialog, int whichButton) {
/* User clicked on a radio button do some
stuff */
}
})
.setPositiveButton(R.string.alert_dialog_ok, new DialogInte
rface.OnClickListener() {
public void onClick(DialogInterface
dialog, int whichButton) {
/* User clicked Yes so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel, newDialo
gInterface.OnClickListener() {
public void onClick(DialogInterface
dialog, int whichButton) {
/* User clicked No so do some stuff */
}
})
.create();
case DIALOG_MULTIPLE_CHOICE:
return new AlertDialog.Builder(AlertDialogsActivit
y.this)
.setIcon(R.drawable.ic_popup_reminder)
.setTitle(R.string.alert_dialog_multi_choice)
.setMultiChoiceItems(R.array.select_dialog_items3,
new boolean[]{false, true, false, true, f
alse, false, false},
new DialogInterface.OnMultiChoiceClickLis
tener() {
public void onClick(DialogInterface
dialog, int whichButton,
boolean isChecked) {
/* User clicked on a check box do
some stuff */
}
})
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface
dialog, int whichButton) {
/* User clicked Yes so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface
dialog, int whichButton) {
/* User clicked No so do some stuff */
}
})
.create();
case DIALOG_MULTIPLE_CHOICE_CURSOR:
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.SEND_TO_VOICEMAIL
};
Cursor cursor
= managedQuery(ContactsContract.Contacts.CONTENT_U
RI,
projection, null, null, null);
return new AlertDialog.Builder(AlertDialogsActi
vity.this)
.setIcon(R.drawable.ic_popup_reminder)
.setTitle(R.string.alert_dialog_multi_choice_cursor)
.setMultiChoiceItems(cursor,
ContactsContract.Contacts.SEND_TO_VOICEMAIL,
ContactsContract.Contacts.DISPLAY_NAME,
new DialogInterface.OnMultiChoiceClick
Listener() {
public void onClick(DialogInterface
dialog, int whichButton,
boolean isChecked) {
Toast.makeText(AlertDialogsActivity.this,
"Readonly Demo Only - Data
will not be updated",
Toast.LENGTH_SHORT).show();
}
})
.create();
case DIALOG_TEXT_ENTRY:
// This example shows how to add a custom layout
to an AlertDialog
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView =
factory.inflate(R.layout.alert_dialog_text_entry, null);
return new AlertDialog.Builder(AlertDialogsActivit
y.this)
.setIconAttribute(android.R.attr.alertDialogIcon)
.setTitle(R.string.alert_dialog_text_entry)
.setView(textEntryView)
.setPositiveButton(R.string.alert_dialog_ok, new DialogInte
rface.OnClickListener() {
public void onClick(DialogInterface
dialog, int whichButton) {