Show Alert Dialog in Android

  • Replies:1
  • OpenNot stickiedUnanswered

Apr 8, 2013 7:56:33 AM via Website

Recently, 9android developer has release new tutorial about alert android. This is three ways to work with alert android dialog box.
View the tutorial source at http://www.9android.net/show-alert-dialog/
Showing Alert Dialog
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.

1<pre><em id="__mceDel">// Creating alert Dialog with one Button
2
3 AlertDialog alertDialog1 = new AlertDialog.Builder(
4 AlertDialogActivity.this).create();
5
6 // Setting Dialog Title
7 alertDialog1.setTitle("Alert Dialog");
8
9 // Setting Dialog Message
10 alertDialog1.setMessage("Welcome to 9Android.net");
11
12 // Setting Icon to Dialog
13 alertDialog1.setIcon(R.drawable.tick);
14
15 // Setting OK Button
16 alertDialog1.setButton("OK", new DialogInterface.OnClickListener() {
17
18 public void onClick(DialogInterface dialog, int which) {
19 // Write your code here to execute after dialog
20 // closed
21 Toast.makeText(getApplicationContext(),
22 "You clicked on OK", Toast.LENGTH_SHORT).show();
23 }
24 });
25
26 // Showing Alert Message
27 alertDialog1.show();

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.

1AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(
2 AlertDialogActivity.this);
3
4// Setting Dialog Title
5alertDialog2.setTitle("Confirm Delete...");
6
7// Setting Dialog Message
8alertDialog2.setMessage("Are you sure you want delete this file?");
9
10// Setting Icon to Dialog
11alertDialog2.setIcon(R.drawable.delete);
12
13// Setting Positive "Yes" Btn
14alertDialog2.setPositiveButton("YES",
15 new DialogInterface.OnClickListener() {
16 public void onClick(DialogInterface dialog, int which) {
17 // Write your code here to execute after dialog
18 Toast.makeText(getApplicationContext(),
19 "You clicked on YES", Toast.LENGTH_SHORT)
20 .show();
21 }
22 });
23// Setting Negative "NO" Btn
24alertDialog2.setNegativeButton("NO",
25 new DialogInterface.OnClickListener() {
26 public void onClick(DialogInterface dialog, int which) {
27 // Write your code here to execute after dialog
28 Toast.makeText(getApplicationContext(),
29 "You clicked on NO", Toast.LENGTH_SHORT)
30 .show();
31 dialog.cancel();
32 }
33 });
34
35// Showing Alert Dialog
36alertDialog2.show();


3. Android alert dialog with Three Button

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

1// Creating alert Dialog with three Buttons
2
3AlertDialog.Builder alertDialog3 = new AlertDialog.Builder(
4 AlertDialogActivity.this);
5
6// Setting Dialog Title
7alertDialog3.setTitle("Save File...");
8
9// Setting Dialog Message
10alertDialog3.setMessage("Do you want to save this file?");
11
12// Setting Icon to Dialog
13alertDialog3.setIcon(R.drawable.save);
14
15// Setting Positive Yes Button
16alertDialog3.setPositiveButton("YES",
17 new DialogInterface.OnClickListener() {
18
19 public void onClick(DialogInterface dialog, int which) {
20 // User pressed Cancel button. Write Logic Here
21 Toast.makeText(getApplicationContext(),
22 "You clicked on YES", Toast.LENGTH_SHORT)
23 .show();
24 }
25 });
26// Setting Positive Yes Btn
27alertDialog3.setNeutralButton("NO",
28 new DialogInterface.OnClickListener() {
29
30 public void onClick(DialogInterface dialog, int which) {
31 // User pressed No button. Write Logic Here
32 Toast.makeText(getApplicationContext(),
33 "You clicked on NO", Toast.LENGTH_SHORT)
34 .show();
35 }
36 });
37// Setting Positive "Cancel" Btn
38alertDialog3.setNegativeButton("Cancel",
39 new DialogInterface.OnClickListener() {
40
41 public void onClick(DialogInterface dialog, int which) {
42 // User pressed Cancel button. Write Logic Here
43 Toast.makeText(getApplicationContext(),
44 "You clicked on Cancel", Toast.LENGTH_SHORT)
45 .show();
46 }
47 });
48// Showing Alert Dialog
49alertDialog3.show();
50break;

— modified on Apr 15, 2013 7:03:51 AM