[Tutorial] Show Alert Dialog in Android

Dialog boxes are a common UI metaphor in desktop and web applications. They ’re used to help users
answer questions, make selections, confirm actions, and read warning or error messages. An Android
Dialog is a floating window that partially obscures the Activity that launched it.
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).

2.1. 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.

// Creating alert Dialog with one Button
 
            AlertDialog alertDialog1 = new AlertDialog.Builder(
                    AlertDialogActivity.this).create();
 
            // Setting Dialog Title
            alertDialog1.setTitle("Alert Dialog");
 
            // Setting Dialog Message
            alertDialog1.setMessage("Welcome to 9Android.net");
 
            // Setting Icon to Dialog
            alertDialog1.setIcon(R.drawable.tick);
 
            // Setting OK Button
            alertDialog1.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
            alertDialog1.show();

2.2. 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 alertDialog2 = new AlertDialog.Builder(
        AlertDialogActivity.this);
 
// Setting Dialog Title
alertDialog2.setTitle("Confirm Delete...");
 
// Setting Dialog Message
alertDialog2.setMessage("Are you sure you want delete this file?");
 
// Setting Icon to Dialog
alertDialog2.setIcon(R.drawable.delete);
 
// Setting Positive "Yes" Btn
alertDialog2.setPositiveButton("YES",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // Write your code here to execute after dialog
                Toast.makeText(getApplicationContext(),
                        "You clicked on YES", Toast.LENGTH_SHORT)
                        .show();
            }
        });
// Setting Negative "NO" Btn
alertDialog2.setNegativeButton("NO",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // Write your code here to execute after dialog
                Toast.makeText(getApplicationContext(),
                        "You clicked on NO", Toast.LENGTH_SHORT)
                        .show();
                dialog.cancel();
            }
        });
 
// Showing Alert Dialog
alertDialog2.show();

Download sample source code: Show Alert Dialog in Android | 9Android | Android Tips, Tricks, Source Code, Reviews

Source: 9Android | Android Tips, Tricks, Sources, Reviews

You might want to check if the Activity is closing when you call the AlertDialog using isFinishing()

Instead of an AlertDialog, I also use a custom Dialog that reconfigures itself with positive/negative buttons and a text input box, depending on what you give it. So you end up with one dialog for all (simple) inputs and errors. Cuts down on code/etc

yes, maybe you’re right