Sunday 2 November 2014

Android Tutorial 17 : Android Alert Dialog Example

What are  Alert Dialogs

An Alert Dialog is used to show some information or to prompts the user to make.
Dialogs can also be used to take input from users, such dialogs are called Customized Dialogs

Buiding  Alert Dialogs:    

 AlertDialog class is used to create Alert Dialog.

Three things are required to create a dialog.
1:Title : The title of the dialog
2: Message: The message to be given in dialog.
3: Image(Optional): Image to displayed in Title Bar


We can also add buttons in Alert Dialog.

Code to create Alert Dialog

AlertDialog dialog;
                dialog = new AlertDialog.Builder(this).create();
                dialog.setTitle("Close");
                dialog.setIcon(android.R.drawable.ic_dialog_info);
                dialog.setMessage("Want to close this App").


dialog.show();


Adding Buttons to Dialog:


dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes",
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which)
            {
                   
                // Your Code
            }   
        });
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No",
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which)
            {
                   
                  // Your Code
            }   
        });
        dialog.show();


Dismissing A dialog 


dialog.dismiss();

No comments:

Post a Comment