Making a list: Coding List Dialogs

  • Written by  Clive

Dialog tutorials download filesMaking a list, checking it twice…List Dialogs

Android list dialog iconYou can use three types of lists in your Alert Dialogs:

  • Single choice lists – users choose one item. There are no buttons. The list is dismissed when an item is selected
  • Persistent lists – the list is dismissed when a button is clicked
    • Persistent lists - Single-choice lists – uses radio buttons. The user can only select one item
    • Persistent lists - Multiple-choice lists – uses checkboxes. The user can select one or more items

Single item list dialog

An example of a Single choice list where the user can select only one item

One at a time: The single list

Creating the list dialog

We create the dialog exactly like we created the Alert Dialog in Coding Android dialogs the easy way:

  • Create the dialog fragment
    • Import the support library class
    • Include an interface to communicate back to the host activity
    • Include the code in the fragment’s onAttach() method which makes sure that the host activity implements the interface
    • Create the dialog

Here’s our code for the interface:

List dialog interface code

The interface calls the host activity’s onClick() method.

There are no action buttons in the Single list dialog. Selecting an item triggers the interface’s OnClickListener().

Here’s the code for building the dialog:

Building the list dialog

Setting up the list dialog is pretty easy. Just set the title and the list of items.

Set up the title for the dialog then set up the list.

The list items appear in the dialog's content area.

We get our list items from an array. You can also load list items from a database using a ListAdapter and a Loader.

  • The transport_array is a String array in the res>values>strings.xml file
  • We use the dialogListener interface to execute the host activity’s onClick() method.
  • We pass the selected item’s position in the list as a parameter to the onClick() method

Selecting a list item

Touching an item selects it and dismisses the dialog. The selected item’s position in the list is passed on to the host activity via the interface.

Cancelling the list dialog

Pressing the back button or touching the screen outside of the dialog will cancel it.

This executes the dialog fragment’s onCancel() method.

Dismissing the list dialog

Touching an item selects it and dismisses the dialog.

It will execute the fragment’s onDismiss() method.

The list dialog’s host activity

As we did in the Alert Dialog in Coding Android dialogs the easy way:

  • Import the support library’s FragmentActivity class
  • Implement the list fragment’s interface

Remember that selecting a list item, sends that item’s position back to the activity. This matches the index of the item in the array.

We can access the array like this:

Accessing resource array

Showing the list dialog

Call show() like this:

Showing the list dialog

Calling show() displays the dialog and adds the ListDialogFragment to the fragment manager. The fragment is identified by its tag, ListDialogFragment

Selecting an item from the list

The selected item’s position in the list is passed on to the host activity via the interface.

We then display the selected item in a Toast message like this:

Host activity interface onClick() method

Use the position of the selected item as the index for the corresponding array item

I hope you found this tutorial helpful.

Here are the links to all the tutorials in this series on Android Dialogs;

This project was created using Android Studio. You can download the project files here.  Download icon

Are you using Eclipse or another IDE? Here's how you can use this project's Android Studio files.