Android AlertDialog Example

By | April 7, 2013
In this tutorial you will see how to create alert dialog box in android ,we are going to see how to creating alert dialog box with one button ,two button and three button ,We also look up on how to theme the alert dialog .

Constructing Alert Dialog :-

To construct a alert dialog box we make use of AlertDialog.Builder class 
AlertDialog.Builder Alert_builder = new AlertDialog.Builder(Context context);

Title : To set title we call “setTitle(CharSequence title)” upon Builder class object
                           Alert_builder.setTitle(“Title”);

Message :To set message we call “setMessage(CharSequence message)” upon Builder class object
                         Alert_builder.setMessage(“Message”);

Icon :  To set Icon we call “setIcon(int iconId)” upon Builder class object
                        Alert_builder.setIcon(R.drawable.alert);

Buttons for Alert Dialog :-

Positive Button :   To set positive button we call  ” setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener) ” upon Builder class object .

        
Negative Button : To set negative button we call “ setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener) ”  upon Builder class object .
   
Netural Button : To set Neutral button we call ” setNeturalButton(CharSequence text, DialogInterface.OnClickListener listener) ”   upon Builder class object .


I have Created an Main activity with three buttons you can find the code with in download zip file below

Android AlertDialog


 
  And Lets see the  creation of the alert dialog box with different number of buttons in it

Alert Dialog with One Button

protected Dialog onCreateDialog(int id) {
   AlertDialog.Builder Alert_builder = new AlertDialog.Builder(AlertDialogDemo.this);
   Alert_builder.setTitle(“AlertDialog_Title”);
   Alert_builder.setMessage(“AlertDialog_Message”);
   Alert_builder.setIcon(R.drawable.alert);
   // adding one button
   Alert_builder.setPositiveButton(“Ok”,
     new DialogInterface.OnClickListener() {

      @Override
      public void onClick(DialogInterface dialog, int which) {
       // do something
      }
     });

   return Alert_builder.create();
   }

Android AlertDialog

Alert Dialog with Two Button

protected Dialog onCreateDialog(int id) {
AlertDialog.Builder Alert_builder = new AlertDialog.Builder(AlertDialogDemo.this);
   Alert_builder.setTitle(“AlertDialog_Title”);
   Alert_builder.setMessage(“AlertDialog_Message”);
   Alert_builder.setIcon(R.drawable.alert);
   // adding two buttons
   // adding ok button
   Alert_builder.setPositiveButton(“Ok”,
     new DialogInterface.OnClickListener() {

      @Override
      public void onClick(DialogInterface dialog, int which) {
       // do something
      }
     });
   // adding cancel button
   Alert_builder.setNegativeButton(“cancel”,
     new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
       // do something
      }
     });

   return Alert_builder.create();
   }

 

Android AlertDialog

Alert Dialog With Three Button

protected Dialog onCreateDialog(int id) {
AlertDialog.Builder Alert_builder = new AlertDialog.Builder(AlertDialogDemo.this);
   Alert_builder.setTitle(“AlertDialog_Title”);
   Alert_builder.setMessage(“AlertDialog_Message”);
   Alert_builder.setIcon(R.drawable.alert);
   // adding three buttons
   // adding ok button
   Alert_builder.setPositiveButton(“Ok”,
     new DialogInterface.OnClickListener() {

      @Override
      public void onClick(DialogInterface dialog, int which) {
       // do something
      }
     });
   // adding cancel button
   Alert_builder.setNegativeButton(“cancel”,
     new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
       // do something
      }
     });

   // adding neutral button
   Alert_builder.setNeutralButton(“Button”,
     new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
       // do something
      }
     });

   return Alert_builder.create();
   }

Android AlertDialog

Themeing Alert Dialog : –

The Builder class constructor actually takes two parameters
  
   AlertDialog.Builder (Context context, int theme) 
   
   Parameter
         context :-The application context
         Theme :- constant that is used to style Dialog  

1. Alert Dialog With THEME_HOLO_DARK

new AlertDialog.Builder(AlertDialogDemo.this,AlertDialog.THEME_HOLO_DARK);

Android AlertDialog

2. Alert Dialog With THEME_HOLO_LIGHT

new AlertDialog.Builder(AlertDialogDemo.this,AlertDialog.THEME_HOLO_LIGHT);

Android AlertDialog

3. Alert Dialog With THEME_TRADITIONAL

new AlertDialog.Builder(AlertDialogDemo.this,AlertDialog.THEME_TRADITIONAL);

Android AlertDialog