5.2+dialogs

Upload: dolby3d

Post on 08-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 5.2+Dialogs

    1/13

    Mobile Application Development (Android)

  • 8/7/2019 5.2+Dialogs

    2/13

    DialogyA small window that pops up on the current Activity to

    alert the user about some action.

    y Focus shifted to the Dialog; user should have toacknowledge that dialog in order to use activity.

    y Example; Progress bar, Login dialog, notificationdialog etc.

    y The Dialog class is the base class for creating dialogs.yYou have to use sub classes of Dialog to notify the user.

  • 8/7/2019 5.2+Dialogs

    3/13

    Dialogy Showing Dialog

    y Dialog belongs to an Activity.

    y Callback method onCreateDialog(int) is available inActivity to create Dialog.

    y the Android system automatically manages the state ofeach dialog and hooks them to the Activity, effectively

    making it the "owner" of each dialog.y showDialog(int) method is used to show the Dialog.

    y onPrepareDialog(int, Dialog) method is called everytime Dialog is created.

  • 8/7/2019 5.2+Dialogs

    4/13

    Dialog

    protected Dialog onCreateDialog(int id) {

    Dialog dialog;switch(id) {

    case DIALOG_1:

    // do the work to define the pause Dialogbreak;

    case DIALOG_2:

    // do the work to define the game over Dialogbreak;

    default:dialog = null;

    }return dialog;

    }

    showDialog(DIALOG_PAUSED_ID);

  • 8/7/2019 5.2+Dialogs

    5/13

    Dialogy Closing a Dialog

    y dismiss() method is used to close the Dialog.

    y Every time your dialog is dismissed, the state of theDialog object is retained by the Activity.

    y removeDialog(int) method is used to remove theinternal references to the Dialog.

  • 8/7/2019 5.2+Dialogs

    6/13

    Dialogy AlertDialog

    y An AlertDialog is an extension of the Dialog class.

    y AlertDialog can be used if dialog have to use any of thefollowing.

    y A title

    y A text message

    y One, two, or three buttons

    y A list of selectable items (with optional checkboxes or radiobuttons)

  • 8/7/2019 5.2+Dialogs

    7/13

    Dialogy AlertDialog

    y Get a Builder with AlertDialog.Builder(Context).

    y se the class's public methods to define all of theAlertDialog properties.

    y After you're done with the Builder, retrieve theAlertDialog object with create().

  • 8/7/2019 5.2+Dialogs

    8/13

    Dialog@Overridepublic 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;

    }

  • 8/7/2019 5.2+Dialogs

    9/13

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

  • 8/7/2019 5.2+Dialogs

    10/13

    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;

    }

  • 8/7/2019 5.2+Dialogs

    11/13

    Dialogy ProgressDialog

    y A ProgressDialog is an extension of the AlertDialog class

    that can display a progress animation in the form of aspinning wheel, for a task with progress that'sundefined, or a progress bar, for a task that has a definedprogression.

    y ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "",

    "Loading. Please wait...", true);

  • 8/7/2019 5.2+Dialogs

    12/13

    Dialogy Progress Bar

    y To show the progression with an animated progress bar:

    y Initialize the ProgressDialog with the class

    constructor, ProgressDialog(Context).y Set the progress style to "STYLE_HORIZONTAL"

    with setProgressStyle(int) and set any other properties, suchas the message.

    y When you're ready to show the dialog, call show() or return

    the ProgressDialog from the onCreateDialog(int) callback.y You can increment the amount of progress displayed in the

    bar by calling either setProgress(int)with a value for the totalpercentage completed so far or incrementProgressBy(int)withan incremental value to add to the total percentage completed

    so far

  • 8/7/2019 5.2+Dialogs

    13/13

    DialogP

    P x

    . P S y P .STY E

    H ! R " Z ! NT#

    .

    M

    " $

    ..."

    . % & '

    y Progress Bar