Thursday, July 19, 2012

Selection list in alert dialog box - Android

- Kalimuthu Rengaswamy
Dialog box is one of the most interesting feature in Android. The style and way making it appear depents upon the type of dialog box used and how well we customize it. This blog explains how to create a list of selectable items in an alert dialog box.

Dialog class in Android
Dialog class has 4 sub classes
1. AlertDialog
2. ProgressDialog : A dialog that displays a progress wheel or progress bar.
3. DatePickerDialog : A dialog that allows the user to select a date.
4. TimePickerDialog : A dialog that allows the user to select a time.

Alert dialog
Alert dialog as the name simplies shows an alert to the user. It can manage at the maximum of 3 buttons, a selectable list of items that can even include multiple choice.

Features of alert dialog
1. A title
2. A text message
3. One, two, or three buttons
4. A list of selectable items (with optional checkboxes or radio buttons)  

Creating alert dialog

Single choice alert:
// this declare in golbal value
public int postion
final AlertDialog.Builder singlechoicedialog = new AlertDialog.Builder(this);
final CharSequence[] Report_items = { "Choice I", "Choice II", "Choice III","Choice IV", "Choice V" };
singlechoicedialog.setTitle("SingleChoice Dialog ");
singlechoicedialog.setSingleChoiceItems(Report_items, -1,
new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int item) {
 postion = item;

 // get selected value
 String value = Report_items[item].toString()
 System.out.println("Selected position::" + value);
AlertDialog alert_dialog = singlechoicedialog.create();;

// set defult select value
alert_dialog.getListView().setItemChecked(postion, true);

Multi choice alert:

final CharSequence[] dilogList = { "Choice One", "Choice Two", "Choice Three" };

AlertDialog.Builder multChoiceDialog = new AlertDialog.Builder(this);

//set title for aleart box

multChoiceDialog.setTitle("MultiChoice Dialog");

boolean[] _selections = new boolean[dilogList.length];

multChoiceDialog.setMultiChoiceItems(dilogList, _selections, new DialogInterface.OnMultiChoiceClickListener() {
 public void onClick(DialogInterface dialog,
 int whichButton, boolean isChecked) {

// add positive button here
multChoiceDialog.setPositiveButton("OK",new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int which) {
 // getting listview from alert box
 ListView list = ((AlertDialog) dialog).getListView();
 StringBuilder sb = new StringBuilder();
 for (int i = 0; i < list.getCount(); i++) {
  boolean checked = list.isItemChecked(i);
  // get checked list value
  if (checked) {
   if (sb.length() > 0)

 Toast.makeText(getApplicationContext(),"Selected digit:"

// add negative button
new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int which) {
 // cancel code here
AlertDialog alert1 = multChoiceDialog.create();;

List alert:

final AlertDialog.Builder menuAleart = new        AlertDialog.Builder(FiltterlistActivity.this);
final String[] menuList = { "function1", "function2" };
menuAleart.setTitle("list dialog");
menuAleart.setItems(menuList,new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int item) {
  switch (item) {
  case 0:
  // function 1 code here
  case 1:
  // function 2 code here
AlertDialog menuDrop = menuAleart.create();;

Here, I have picked a single feature of alert dialog and explained. You can just copy, paste and execute the code for the results.  


Anonymous said...

Nice One Dude.. Its really help me ..keep the good work ..:)

Saru's creation said...

congrats. da.. i shared ur link to my friens.. :)

Putra Nugraha said...

very nice, it's help me
thanks... :)