Saturday, August 10, 2013

Alert Dialog Box

Alert Dialog box is used to confirm further execution from user. For Example if some data needs to delete,then first of all we have to set confirmation dialog and let user choose whether he/she wants to delete data or not.

There are basically 3 types of dialog box.

  1. One button dialog box - It uses when only one option available or we can also print some message on it.
  2. Two button dialog box - As above discussion, If user needs to choose one option. like yes/no, OK/cancel etc.
  3. Three button dialog box - It uses positive,negative and neutral button.For Example Save file dialog box-it has three buttons - YES, NO, CANCEL.

Lets see the code for these dialog box.

1. ONE Button dialog box.

First of all  create on project and put below code where you want to show dialog box.

CODE:


  AlertDialog ad = new AlertDialog.Builder(MainActivity.this).create();
        ad.setTitle("Alert Dialog");
        ad.setMessage("Message Part");
        ad.setIcon(R.drawable.ic_launcher);
       
        ad.setButton("OKEY",
        new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"You clicked on OKey", Toast.LENGTH_SHORT)
.show();
}
});
       
ad.show();



2. TWO Button dialog box.

CODE:


    AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);

 


        alertDialog.setTitle("Confirm Delete...");

        alertDialog.setMessage("Are you sure you want delete this?");

        alertDialog.setIcon(R.drawable.delete);
        // Setting Positive "YES" Button

        alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog,int which) {
 

            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) {
       
     Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
           dialog.cancel();
           }
       });
 
       // Showing Alert Message
       alertDialog.show();



3. THREE Button dialog box.


CODE:


AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.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();

Download Full Source Code: Download


No comments:

Post a Comment