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.
| AlertDialog alertDialog = newAlertDialog.Builder(                        AlertDialogActivity.this).create();        // Setting Dialog Title        alertDialog.setTitle("Alert Dialog");        // Setting Dialog Message        alertDialog.setMessage("Welcome to AndroidHive.info");        // Setting Icon to Dialog        alertDialog.setIcon(R.drawable.tick);        // Setting OK Button        alertDialog.setButton("OK", newDialogInterface.OnClickListener() {                publicvoidonClick(DialogInterface dialog, intwhich) {                // 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(); | 
This output of about code will be like following image.

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 = newAlertDialog.Builder(AlertDialogActivity.this);        // Setting Dialog Title        alertDialog.setTitle("Confirm Delete...");        // Setting Dialog Message        alertDialog.setMessage("Are you sure you want delete this?");        // Setting Icon to Dialog        alertDialog.setIcon(R.drawable.delete);        // Setting Positive "Yes" Button        alertDialog.setPositiveButton("YES", newDialogInterface.OnClickListener() {            publicvoidonClick(DialogInterface dialog,intwhich) {            // Write your code here to invoke YES event            Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();            }        });        // Setting Negative "NO" Button        alertDialog.setNegativeButton("NO", newDialogInterface.OnClickListener() {            publicvoidonClick(DialogInterface dialog, intwhich) {            // 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(); | 
This output of about code will be like following image.

Android alert dialog with three button
Here setNeutralButton() is used to create a neutral cancel button
| AlertDialog.Builder alertDialog = newAlertDialog.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", newDialogInterface.OnClickListener() {                    publicvoidonClick(DialogInterface dialog, intwhich) {                    // 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", newDialogInterface.OnClickListener() {                    publicvoidonClick(DialogInterface dialog, intwhich) {                    // 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", newDialogInterface.OnClickListener() {                    publicvoidonClick(DialogInterface dialog, intwhich) {                    // User pressed Cancel button. Write Logic Here                    Toast.makeText(getApplicationContext(), "You clicked on Cancel",                                        Toast.LENGTH_SHORT).show();                    }                });                // Showing Alert Message                alertDialog.show(); | 
This output of about code will be like following image.

 
 
 
No comments:
Post a Comment