Here’s an example of a multiple-choice list dialog:
The Multiple-choice list lets you choose more than one item.
Let’s see how we create the multiple-choice list.
Creating the multiple-choice list dialog fragment
Basically the steps for creating the multiple-choice list are the same as creating an Alert Dialog:
- Import the support library’s dialog fragment class
- Create an interface to communicate back to the host activity
- Include the code in the fragment’s onAttach() method to make sure that the host activity uses the interface
Here’s the code for the interface:
Note the following:
- selectedItemsIndexList – an array list of type Integer. It stores the selected items position in the list
- Our listener contains two methods which can be found in the host activity (onOkay() and onCancel()). These will be called when either dialog button is clicked
Creating the multiple-choice list dialog
Here’s the code. Note the following:
- We use an arrayList to save each of the selected item’s position in the list
- isSelectedArray is an array of boolean objects (true or false), one for each of the list items. It sets the list items to checked or unchecked when the list is first displayed
- We use the AlertDialog.Builder to build the dialog
- setMultiChoiceItems() – sets the list items to display in the dialog. It’s parameters are:
- an array of country names
- a boolean array of which items should be checked or unchecked by default
- a listener to handle the item selection
- onClick() – this method is executed each time the user checks or unchecks a box. The position of the item is added to the arrayList when checked and removed from the arrayList when unchecked
Setting the multiple-choice list dialog buttons
We have two buttons.
The positive button
The Okay button; clicking this button saves the selections.
It dismisses the dialog and sends the array list containing the selected items positions to the host activity.
The fragment’s onDismiss() method would also be called.
The negative button
The Cancel button; clicking this button cancels the dialog.
The fragment’s onCancel() and onDismiss() methods would be called.
The host activity’s onCancel() method is called via the interface.
The fragment's onCancel() and onDismiss() methods
You can include these methods in the dialog fragment if you need them.
Place any additional code you want executed when the dialog is cancelled or dismissed in these methods.
The multiple-choice list dialog’s host activity
As for the Alert Dialog:
- Import the support library’s FragmentActivity class
- Implement the list fragment’s interface
Create a String array object, countriesArray which we’ll use to display the selected list items.
Here’s how we get the array from the resource file:
Showing the multiple-choice list dialog
Here’s the code for showing the dialog:
- Create a new instance of the dialog fragment, MultiChoiceListDialogFragment
- Calling show() displays the dialog and adds the fragment to the fragment manager. The fragment is identified by the tag, MultiChoiceListDialogFragment which is passed as a parameter (we can then access the fragment at any time by calling findFragmentByTag() passing the tag as parameter)
Getting the selected items
When the user presses the Okay button, the interface calls the host activity’s onOkay() method, passing the array list as a parameter.
The array list contains the positions of the selected items. These integers correspond to the indexes of the items in the String array.
We can then loop through the array and build a string containing all the selected countries.
- The if statement checks if the array list is not empty (all the boxes were unchecked). If it’s empty then we do nothing
- We use the array list’s position items as indexes to the array items
- StringBuilder – unlike Strings, contains a modifiable sequence of characters. We use it to build a string containing all the selected countries
- When we’re finished, we display all the selected countries in a Toast message
Cancelling the dialog
Cancelling the dialog calls the host activity’s onCancel() method. This displays a Toast message.
You could also include an onCancel() method in the fragment. This will also execute when the dialog is cancelled.
I hope you found this tutorial helpful.
Here are the links to all the tutorials in this series on Android Dialogs;
- Coding Android dialogs the easy way
- Making a list: Coding List Dialogs
- Making a list: Coding Multiple-choice List Dialogs
- Coding Android Custom Dialogs the easy way
- Coding Android Activity dialogs the easy way
This project was created using Android Studio. You can download the project files here.
Are you using Eclipse or another IDE? Here's how you can use this project's Android Studio files.