How To Display A Dialog Window In Android

In this article, I will explain about displaying a dialog window. There are many times when a user needs to display a dialog window to get a confirmation from the end user. To create an environment for Android application development and get an idea about activities life cycle, refer to the below articles,

Create a project,

 

And finally,



Add a button in activity_main.xml file,
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  2. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    
  3. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"    
  4. android:paddingRight="@dimen/activity_horizontal_margin"    
  5. android:paddingTop="@dimen/activity_vertical_margin"    
  6. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">    
  7.     
  8. <Button    
  9. android:id="@+id/btnDialog"    
  10. android:layout_width="fill_parent"    
  11. android:layout_height="wrap_content"    
  12. android:text="click to display a dialog"    
  13. android:onClick="onClick"    
  14. />    
  15.     
  16. </LinearLayout>    

In MainActivity.java class, add an array,

  1. CharSequence[] items={"Wipro""IBM""TCS"};    
  2. boolean[] itemChecked = new boolean [items.length];    

To create a dialog window, override the onCreateDialog() protected method which is defined in the Activity base class.

  1. protected Dialog onCreateDialog(int id){    
  2.      switch(id) {    
  3.          case 0:    
  4.              return new AlertDialog.Builder(this)    
  5.                      .setIcon(R.drawable.i1)    
  6.                      .setTitle("This is a simple dialog!")    
  7.                      .setPositiveButton("OK",    
  8.                              new DialogInterface.OnClickListener() {    
  9.                                  @Override    
  10.                                  public void onClick(DialogInterface dialogInterface, int i) {    
  11.                                      Toast.makeText(getBaseContext(), "OK clicked!", Toast.LENGTH_SHORT).show();    
  12.                                  }    
  13.                              }    
  14.                      )    
  15.                      .setNegativeButton("Cancel",    
  16.                              new DialogInterface.OnClickListener() {    
  17.                                  @Override    
  18.                                  public void onClick(DialogInterface dialogInterface, int i) {    
  19.                                      Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_SHORT).show();    
  20.                                  }    
  21.                              }    
  22.                      )    
  23.                      .setMultiChoiceItems(items, itemChecked,    
  24.                              new DialogInterface.OnMultiChoiceClickListener() {    
  25.                                  @Override    
  26.                                  public void onClick(DialogInterface dialogInterface, int i, boolean b) {    
  27.                                      Toast.makeText(getBaseContext(),items[i]+ (b?" is checked":" is unchecked"),Toast.LENGTH_SHORT).show();    
  28.                                  }    
  29.                              }    
  30.      
  31.                      ).create();    
  32.      }    
  33.                  return null;    
  34.      }   

Below method is called when you call the showDialog() method.

  1. public void onClick(View v)    
  2.  {    
  3.      showDialog(0);    
  4.  }    

The onCreateDialog() method is a callback for creating dialogs that are managed by the activity. When showDialog() method is called, this callback onCreateDialog() method is invoked. The showDialog() method accepts an integer as an argument to display a particular dialog. We have used switch statement to identify the different types of dialogs to create. To create a dialog, AlertDialog class, Builder constructor is used.

In the above code, OK and Cancel are sets using the setPositionButton() and setNegativeButton(). A user has the option to select multiple options by using setMultipleChioceItems() method. When an option is checked or unchecked by the user, he/she gets the message that selected option is checked or unchecked.

 

Click on above button display in the screenshot,

 

If user unchecks the option,

 
 
In the next article, I will explain about Progress Dialog.

Up Next
    Ebook Download
    View all
    Learn
    View all