Provide Selection from a list of options – Android

Home / Android / Provide Selection from a list of options – Android

This post is second in series of tutorials to create and display an AlertDialog. Before starting this post, let us recap few things from previous post.

  • To create an AlertDialog, we need AlertDialog.Builder and provide all required properties through the builder instance.
  • Buttons are optional in AlertDialog, we can use all combinations of the buttons or we can choose not to use any button at all.
  • AlertDialog can be created without any title.
  • AlertDialog can be created without any message.

Series Index of AlertDialog

  1. Create AlertDialog and Basic Usage
  2. Provide Selection from a list of options
  3. Create AlertDialog with Custom Layout using XML Layout
  4. Create AlertDialog with custom layout programmatically
  5. Create AlertDialog with complex custom layout programmatically
  6. Override Button of AlertDialog

In this post we will be using the code created in the previous post. You can either revisit the previous post or get the source code from GitHub. That’s why this post will contain only methods to display the AlertDialog. All boiler-plate code could be taken from the previous post.

In this tutorial we will be creating AlertDialog which will provide options (single/multiple selection) to select from.




Display Multi-choice Option Dialog

Let’s take a scenario, display user a list of colors in the rainbow and ask to select primary colors. For this, we will be creating a dialog and setting colors using setMultiChoiceItems(). This will display the list of colors with CheckBox, hence allowing user to select multiple options from the list. After completing the selection, user can click the Done button of the dialog to close it. For this, we have to keep few extra variables

  1. boolean[] checkedItems will keep track of all the selected items, so that if you open the dialog again, previously selected options will remain selected
  2. String[] colors will provide the array of colors in the rainbow, which will be used to first display the items in the AlertDialog and then for putting the selected color into the list.
  3. List<String> selectedColors will hold the name of selected color(s) in the list.

Do not use setMessage() when displaying choice items. An AlertDialog can display either a message or the choice items. And, if both are used, then message gets priority over choice options.

private boolean[] checkedItems = new boolean[7];
private String[] colors;
private List<String> selectedColors = new ArrayList<>();

private void displayMultiSelectDialog() {
    colors = getResources().getStringArray(R.array.rainbow_colors);
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    dialogBuilder.setTitle("Select primary colors");
    dialogBuilder.setMultiChoiceItems(colors, checkedItems,
            (dialogInterface, which, isSelected) -> {
                if (isSelected) {
                    selectedColors.add(colors[which]);
                } else {
                    selectedColors.remove(colors[which]);
                }
            }
    );
    dialogBuilder.setPositiveButton("Done", (dialog, which) -> showSelectedColors());
    dialogBuilder.create().show();
}

private void showSelectedColors() {
    // do whatever you want to do with the user choice(s)
    Toast.makeText(this, selectedColors.toString(), Toast.LENGTH_SHORT).show();
}

display alertdialog display alertdialog display alertdialog display alertdialog

Display Single-choice Option Dialog

For instance, in a feedback users could be asked to select the version of Android they are currently using. This will need the AlertDialog to display a list of Android versions to choose from with RadioButton. After selecting the version user can click the Done button to close the AlertDialog. For this implementation we have to maintain two extra fields.

  1. int checkedItem: to keep track of last checked item and automatically select the last selected item.
  2. String[] androidVersions: to provide a list of Android versions to choose from.
private int checkedItem;
private String[] androidVersions;

private void displaySingleSelectionDialog() {
    androidVersions = getResources().getStringArray(R.array.android_versions);
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    dialogBuilder.setTitle("Which version you are using?");
    dialogBuilder.setSingleChoiceItems(androidVersions, checkedItem,
            (dialogInterface, which) -> {
                checkedItem = which;
            });
    dialogBuilder.setPositiveButton("Done", (dialog, which) -> showSelectedVersion());
    dialogBuilder.create().show();
}

private void showSelectedVersion() {
    Toast.makeText(this, "You selected: " + androidVersions[checkedItem], Toast.LENGTH_SHORT).show();
}

display alertdialog display alertdialog display alertdialog display alertdialog

Download Source Code

Download Source Code