You are on page 1of 13

Mobile Application Development (Android)

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

 One, two, or three buttons

 A list of selectable items (with optional checkboxes or radio

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"};

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


builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item],
Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
return alert;
}
Dialog
@Override
public Dialog onCreateDialog(int id){
final CharSequence[] items = {"Red", "Green", "Blue"};

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


builder.setTitle("Pick a color");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item],
Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
return alert;
}
Dialog
ProgressDialog
A ProgressDialog is an extension of
the AlertDialog class that can display a progress
animation in the form of a spinning wheel, for a task
with progress that's undefined, or a progress bar, for a
task that has a defined progression.
 ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "",
                        "Loading. Please wait...", true);
Dialog
Progress Bar
To show the progression with an animated progress bar:
 Initialize the ProgressDialog with the class
constructor, ProgressDialog(Context).
 Set the progress style to "STYLE_HORIZONTAL"

with setProgressStyle(int) and set any other properties, such as the


message.
 When you're ready to show the dialog, call show() or return the

ProgressDialog from the onCreateDialog(int) callback.


 You can increment the amount of progress displayed in the bar by

calling either setProgress(int) with a value for the total percentage


completed so far or incrementProgressBy(int) with an incremental
value to add to the total percentage completed so far
Dialog
Progress Bar
ProgressDialog progressDialog;
progressDialog = new ProgressDialog(mContext);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);

You might also like