Android List Dialog Example

Android list dialog is used to show the list of items inside a dialog box. It is similar to AlertDialog which i explained in my previous tutorial Android AlertDialog Example ,but will contain a list of items in which user can pick one item or it can be multichoice list as well. In this tutorial we will learn how to implement list dialog in android.

It is as simple as adding the following method to your Activity’s java file and call that method on your action for showing list dialog.

public void CreateAlertDialog() {
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

alertbox.setTitle("Pick one item")
.setItems(R.array.alert, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int pos) {

//pos will give the selected item position

}
});
alertbox.show();
}

Now you need to have an array in your strings.xml file

<string-array name="alert">
<item>item 1</item>
<item>item 2</item>
<item>item 3</item>
</string-array>

Expected output:

list-dialog
list-dialog

Next we will see how to implement multichoice list in dialog. This will contain  a positive and negative button as well to make the action with selected items in list. Just Replace the above method with below to make list as multichoice list. Whenever an item is checked it will be added to the array list. And similarly if unchecked it will be removed from arraylist . Finally while clicking on ok button we can fetch the items in array list and can do the action which is required to do with selected items.

public void CreateAlertDialog() {

final ArrayList<String> selected_items = new ArrayList<>();
final String[] items_array = getResources().getStringArray(R.array.alert);
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertbox.setTitle("Select Items")
.setMultiChoiceItems(R.array.alert, null,new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int pos, boolean isChecked) {

if (isChecked) {

//add item to arraylist
selected_items.add(items_array[pos]);
}

else if (selected_items.contains(items_array[0])) {
// Else, if the item is already in the array, remove it
for(int i = 0;i< selected_items.size();i++){
if(selected_items.get(i).equalsIgnoreCase(items_array[i])){

selected_items.remove(i);

}
}
}
}
})
// Set the action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {

// do waterver you want to do with arraylist
}
})

.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//do nothing, dialog will dismiss automatically
}
});
alertbox.show();
}

The output will be

multichoice-list-dialog
multichoice-list-dialog

 

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

Add a Comment

Your email address will not be published. Required fields are marked *