Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener. The list will have a check mark displayed to the right of the text for each checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a button will dismiss the dialog.
Parameters:
items - the text of the items to be displayed in the list.
checkedItems - specifies which items are checked. It should be null in which case no items are checked. If non null it must be exactly the same length as the array of items.
listener - notified when an item on the list is clicked. The dialog will not be dismissed when an item is clicked. It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog.
Returns: This Builder object to allow for chaining of calls to set methods
1. Create an AlertDialog with Supplied Arguments
2. Initialize a CharSequence Array
3. Add the line below to the imports section.
import android.content.DialogInterface;
4. After your AlertDialog.Builder is instantiated in step 1, add the following code to set a list of items with a check box to choose one or many of the options. This will display the myCharSequence array in the list. Once you select an item, the dialog box does NOT automatically close. You may wish to Add a Positive Button to close the AlertDialog or maybe add a dismiss(). The null signifies that none of the items will be selected by default.
myAlertDialogBuilder.setMultiChoiceItems(myCharSequence, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
//To test, Add a toast, with a context of getBaseContext() and a message such as
// "You selected "+ myCharSequence[which] + " Checked?: " + isChecked
}
});
5. Add an AlertDialog.Builder Positive Button Listener. This will enable the user to close the AlertDialog box after the item selections are made.
User selects the first option |
Next Recommended Article: Declare an AlertDialog object