[Guidle]Show Alert Dialog in Android

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

[Guidle]Show Alert Dialog in Android

Postby nickmoi451 » Mon Apr 08, 2013 4:06 am

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.

Image
Hide content

<pre>
Code: Select all
<em id="__mceDel">// 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. 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.

Image
Code: Select all

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();


Donwload code: http://www.dl.9android.net/index.php?ac ... 1365169978
Source: http://www.9android.net/show-alert-dialog/
nickmoi451
Freshman
Freshman
 
Posts: 6
Joined: Mon Apr 08, 2013 2:12 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 11 guests