Thursday 26 September 2013

Android alert dialog example

OverView:

 In this tutorial i will show that how to create a alert dialog with multiple choice(Single Button,Two Button,Three Button).

Dialog:  

A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed.Dialogs prompt the user for decisions or additional information required by the application to continue a task. Such requests can range from simple Cancel/OK decisions to more complex layouts asking the user to adjust settings or enter text.

Lets takes a simple example to show dialog concept:
  • Create a new project File -> New -> Android Application Project.Now go to “res/layout" and paste the following code under main.xml file.
  • You can give any name to your xml file,In my case it is main.xml.
  • This is a single xml file that will give you all view for alert dialog in this app.
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   

<Button
        android:id="@+id/btn_alert1"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="40dip"
        android:padding="20dip"
        android:textSize="17sp"
        android:text="Show Alert With Single Button "
        android:textColor="#00008B" >
    </Button>

    <Button
        android:id="@+id/btn_alert2"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:padding="20dip"
        android:textSize="17sp"
        android:text="Show Alert With Two Buttons"
        android:textColor="#00008B" >
    </Button>

    <Button
        android:id="@+id/btn_alert3"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:padding="20dip"
        android:textSize="17sp"
        android:text="Show Alert With Three Buttons"
        android:textColor="#00008B" >
    </Button>

</LinearLayout> 


This will be your main Application Screen:



Now the Java code for your App(Alert dialog with single button):

 This code will create a simple alert dialog with one button. Mainly here is three important methods one is setTitle() method used for set Title to alert dialog. setMessage() is used for setting message to alert dialog. setIcon() is to set icon to alert dialog.This code only show a single button name OK.

     AlertDialog alertDialog = new AlertDialog.Builder(
                        AlertDialogActivity.this).create();

                // Here i am setting Dialog Title
                alertDialog.setTitle("Alert Dialog");

                // now setting Dialog Message
                alertDialog.setMessage("Welcome to AndroidTutorialRkt");

                // here setting Icon to Dialog
                alertDialog.setIcon(R.drawable.tick);

                // here setting OK Button
                alertDialog.setButton("OK",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {

                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on OK", Toast.LENGTH_LONG)
                                        .show();
                            }
                        });

                // Showing Alert Message
                alertDialog.show();



Output for above Code:



Alert dialog with two button:

Below code create two button's in alert dialog,one is YES button and other is NO button.

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

                // For "Yes" Button
                alertDialog.setPositiveButton("YES",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on YES",
                                        Toast.LENGTH_LONG).show();
                            }
                        });
                // Setting Negative "NO" Button
                alertDialog.setNegativeButton("NO",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on NO", Toast.LENGTH_LONG)
                                        .show();
                                dialog.cancel();
                            }
                        });

                // To show the alert msg
                alertDialog.show();  


 Output for above Code:




Alert dialog with three button:

Below code create three button's in alert dialog,1st is YES button  2nd is NO button and 3rd is CANCEL button.



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

                // Set Dialog Title here
                alertDialog.setTitle("Save File.....");

                // Set Dialog Message here
                alertDialog.setMessage("Do you want to save this file?");

                // Set Icon to Dialog here
                alertDialog.setIcon(R.drawable.save);

                // Setting Positive Yes Button
                alertDialog.setPositiveButton("YES",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on YES",
                                        Toast.LENGTH_LONG).show();
                            }
                        });
                // Setting Positive Yes Button
                alertDialog.setNeutralButton("NO",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on NO", Toast.LENGTH_LONG)
                                        .show();
                            }
                        });
                // Setting Positive "Cancel" Button
                alertDialog.setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on Cancel",
                                        Toast.LENGTH_LONG).show();
                            }
                        });
                // Showing Alert Message
                alertDialog.show();
  



 Output for above Code: 




Now the complete code for this app: 

Open your main Java activity class and copy/paste below given code in it,You can assign any name to your Java class as you wants but here i am taking it AlertDialogActivity.java.

 package com.rkt.alertdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AlertDialogActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btnAlertSingle = (Button) findViewById(R.id.btn_alert1);
        Button btnAlert_2Btns = (Button) findViewById(R.id.btn_alert2);
        Button btnAlert_3Btns = (Button) findViewById(R.id.btn_alert3);

        btnAlertSingle.setOnClickListener(new View.OnClickListener() {

            @SuppressWarnings("deprecation")
            public void onClick(View arg0) {
                // First step to create alert Dialog with one Button

                AlertDialog alertDialog = new AlertDialog.Builder(
                        AlertDialogActivity.this).create();

                // Here i am setting Dialog Title
                alertDialog.setTitle("Alert Dialog");

                // now setting Dialog Message
                alertDialog.setMessage("Welcome to AndroidTutorialRkt");

                // here setting Icon to Dialog
                alertDialog.setIcon(R.drawable.tick);

                // here setting OK Button
                alertDialog.setButton("OK",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {

                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on OK",
                                        Toast.LENGTH_LONG).show();
                            }
                        });

                // Showing Alert Message
                alertDialog.show();

            }
        });

        btnAlert_2Btns.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // Now for alert Dialog with two Buttons

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

                // For "Yes" Button
                alertDialog.setPositiveButton("YES",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on YES",
                                        Toast.LENGTH_LONG).show();
                            }
                        });
                // Setting Negative "NO" Button
                alertDialog.setNegativeButton("NO",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on NO",
                                        Toast.LENGTH_LONG).show();
                                dialog.cancel();
                            }
                        });

                // To show the alert msg
                alertDialog.show();

            }
        });

        btnAlert_3Btns.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // Creating alert Dialog with three Buttons

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

                // Set Dialog Title here
                alertDialog.setTitle("Save File.....");

                // Set Dialog Message here
                alertDialog.setMessage("Do you want to save this file?");

                // Set Icon to Dialog here
                alertDialog.setIcon(R.drawable.save);

                // Setting Positive Yes Button
                alertDialog.setPositiveButton("YES",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on YES",
                                        Toast.LENGTH_LONG).show();
                            }
                        });
                // Setting Positive Yes Button
                alertDialog.setNeutralButton("NO",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on NO",
                                        Toast.LENGTH_LONG).show();
                            }
                        });
                // Setting Positive "Cancel" Button
                alertDialog.setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Toast to display msg on button click
                                Toast.makeText(getApplicationContext(),
                                        "You have clicked on Cancel",
                                        Toast.LENGTH_LONG).show();
                            }
                        });
                // Showing Alert Message
                alertDialog.show();

            }
        });
    }
}







Please Share this tutorial On:


5 comments: