Professional Documents
Culture Documents
Dialog
A small window that pops up on the current Activity
to alert the user about some action.
Focus shifted to the Dialog; user should have to
acknowledge that dialog in order to use activity.
Example; Progress bar, Login dialog, notification
dialog etc.
The Dialog class is the base class for creating dialogs.
You have to use sub classes of Dialog to notify the
user.
Dialog
Showing Dialog
Dialog belongs to an Activity.
Callback method onCreateDialog(int) is available in
Activity to create Dialog.
the Android system automatically manages the state of
each dialog and hooks them to the Activity, effectively
making it the "owner" of each dialog.
showDialog(int) method is used to show the Dialog.
onPrepareDialog(int, Dialog) method is called every
time Dialog is created.
Dialog
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch(id) {
case DIALOG_1:
// do the work to define the pause Dialog
break;
case DIALOG_2:
// do the work to define the game over Dialog
break;
default:
dialog = null;
}
return dialog;
}
showDialog(DIALOG_PAUSED_ID);
Dialog
Closing a Dialog
dismiss() method is used to close the Dialog.
Every time your dialog is dismissed, the state of the
Dialog object is retained by the Activity.
removeDialog(int) method is used to remove the
internal references to the Dialog.
Dialog
AlertDialog
An AlertDialog is an extension of the Dialog class.
AlertDialog can be used if dialog have to use any of the
following.
A title
A text message
buttons)
Dialog
AlertDialog
Get a Builder with AlertDialog.Builder(Context).
se the class's public methods to define all of the
AlertDialog properties.
After you're done with the Builder, retrieve the
AlertDialog object with create().
Dialog
@Override
public Dialog onCreateDialog(int id){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intents_Activity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
return alert;
}
Dialog
@Override
public Dialog onCreateDialog(int id){
final CharSequence[] items = {"Red", "Green", "Blue"};