Tuesday, February 10, 2015

How to Show Alert Dialog in Android

Android Alert Dialog is one of the most important and basic component in Android applications.  This dialog is used to show alerts to the users, get confirmation from the users.
There are three parts in the alert dialog.
      -Title
      -Content
     - Action Button(s)
In these three parts, ‘Title’ is an optional part.
Content:
This part is used to show the message to the user. In the sample alert dialog shown above ‘Sample One Action Button Alert’ is the content.
This content can be a string message or a list or any custom layout.
Action Button:
This is one of the important parts in the alert dialog. This is used to interact with the user. In the sample alert dialog shown above ‘OK’ is the action button.
Action buttons can be any one of the below three types;
Positive action button
Negative action button
Neutral action button

You can add all three types of action buttons to the Android alert dialog. It depends on the need. But you cannot add more than three action buttons in an alert dialog. Which means a particular type of action button cannot be added more than once.
So an alert dialog can have maximum of one positive, one negative and one neutral action button.
Normally positive action button is used to accept the action. (OK/YES)
Negative action is used to cancel the action. (NO)
Neutral action is used in neutral situations (May be later)

Creating alert dialog is very easy. In this tutorial i will be discussing about creating different alert dialogues with one button(ok button), two buttons(yes or no buttons) and three buttons(yes, no and cancel buttons).

Android alert dialog with One button

The following code will create a simple alert dialog with one button. In the following code setTitle() method is used for set Title to alert dialog. setMessage() is used for setting message to alert dialog. setIcon() is to set icon to alert dialog.

Write below code where you want alert dialog box in your project.

AlertDialog alertDialog = new AlertDialog.Builder(AlertDialogActivity.this).create();
        // Setting Dialog Title
        alertDialog.setTitle("Succesfully send!");
        // Setting Dialog Message
        alertDialog.setMessage("Check your email and verify your username and password...");
        // Setting Icon to Dialog
        alertDialog.setIcon(R.drawable.icon);
        // Setting OK Button
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                // Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT)
.show();
                }
        });
        // Showing Alert Message
        alertDialog.show();



Android alert dialog with two button

The following code will create alert dialog with two button. setPositiveButton() is used to create a positive button in alert dialog and setNegativeButton() is used to invoke negative button to alert dialog.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);
        // Setting Dialog Title
        alertDialog.setTitle("Set Title here");
        // Setting Dialog Message
        alertDialog.setMessage("Set the Text Message here");
        // Setting Icon to Dialog
        alertDialog.setIcon(R.drawable.delete);
        // Setting Positive "Yes" Button
        alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
            // Write your code here to invoke YES event
            Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
            }
        });
        // Setting Negative "Cancel" Button
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            // Write your code here to invoke NO event
            Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
            dialog.cancel();
            }
        });
        // Showing Alert Message
        alertDialog.show();



Android alert dialog with three button

Here setNeutralButton() is used to create a neutral cancel button

AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);
                // Setting Dialog Title
                alertDialog.setTitle("Save File...");
                // Setting Dialog Message
                alertDialog.setMessage("Do you want to save this file?");
                // Setting Icon to Dialog
                alertDialog.setIcon(R.drawable.save);
                // Setting Positive "Yes" Button
                alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    // User pressed YES button. Write Logic Here
                    Toast.makeText(getApplicationContext(), "You clicked on YES",
                                        Toast.LENGTH_SHORT).show();
                    }
                });
                // Setting Negative "NO" Button
                alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    // User pressed No button. Write Logic Here
                    Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
                    }
                });
                // Setting Netural "Cancel" Button
                alertDialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    // User pressed Cancel button. Write Logic Here
                    Toast.makeText(getApplicationContext(), "You clicked on Cancel",
                                        Toast.LENGTH_SHORT).show();
                    }
                });
                // Showing Alert Message
                alertDialog.show();

No comments:

Post a Comment