Android
Alert Dialog
ListView
Android Development
User Interface

How can I display a list view in an Android Alert Dialog?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Displaying a list inside an Android AlertDialog is a common pattern for quick choice menus, filters, and simple pickers. The right implementation depends on how complex the list needs to be: setItems is great for a basic list, setAdapter gives more control, and a custom view is better when the dialog needs richer UI behavior.

The Simplest Option: setItems

If all you need is a short list of text choices, AlertDialog.Builder.setItems(...) is the cleanest solution.

kotlin
1val items = arrayOf("Red", "Green", "Blue")
2
3AlertDialog.Builder(this)
4    .setTitle("Pick a color")
5    .setItems(items) { _, which ->
6        val selected = items[which]
7        println("Selected: $selected")
8    }
9    .setNegativeButton("Cancel", null)
10    .show()

This already gives you a dialog with a list-like presentation and item click callback. For many use cases, you do not need to create a literal ListView yourself.

Using setAdapter for More Control

If you want a custom adapter or more control over row rendering, use setAdapter.

kotlin
1val items = listOf("One", "Two", "Three")
2val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, items)
3
4AlertDialog.Builder(this)
5    .setTitle("Choose an item")
6    .setAdapter(adapter) { _, which ->
7        val selected = items[which]
8        println(selected)
9    }
10    .show()

This still uses the dialog’s built-in list handling, but it lets you choose the row layout and adapter behavior.

When You Truly Need a ListView

A real embedded ListView makes sense when the dialog content is more customized than the builder shortcuts allow.

kotlin
1val listView = ListView(this)
2val items = listOf("Apple", "Banana", "Cherry")
3listView.adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, items)
4
5val dialog = AlertDialog.Builder(this)
6    .setTitle("Fruits")
7    .setView(listView)
8    .setNegativeButton("Close", null)
9    .create()
10
11listView.setOnItemClickListener { _, _, position, _ ->
12    println("Clicked: ${items[position]}")
13    dialog.dismiss()
14}
15
16dialog.show()

This approach is more flexible, but it also means you are responsible for wiring the list behavior yourself.

Prefer DialogFragment for Lifecycle Safety

If the dialog is more than a quick inline prompt, consider showing it from a DialogFragment. That keeps the dialog tied more cleanly to fragment lifecycle and configuration changes.

For simple examples, an inline AlertDialog.Builder is fine. For production dialogs that can survive rotation or integrate with a ViewModel, DialogFragment is often the better long-term design.

Single-Choice and Multi-Choice Lists

If the dialog represents a selection state rather than a plain click action, AlertDialog.Builder also has dedicated APIs such as setSingleChoiceItems and setMultiChoiceItems. Those are often a better fit than embedding a raw ListView, because the builder manages the checked-state UI for you. In other words, pick the dialog API that matches the interaction pattern before reaching for a custom view.

Performance and UX Considerations

A dialog is good for short or medium lists. For long, searchable, or highly interactive lists, a full screen or bottom sheet often provides a better experience than squeezing a complex list into an alert dialog.

That is an important design question, not just an implementation detail. The technically possible UI is not always the best UI.

Common Pitfalls

  • Building a custom ListView when setItems would have solved the problem adds unnecessary code.
  • Putting a very large list in an alert dialog makes the UI harder to use.
  • Forgetting to dismiss the dialog after item selection can feel awkward for one-shot choices.
  • Using an activity context incorrectly from detached fragments can cause dialog errors.
  • Ignoring configuration changes makes inline dialog code harder to maintain over time.

Summary

  • Use setItems for the simplest text-choice dialog.
  • Use setAdapter when you need adapter-level customization.
  • Use a custom ListView only when the built-in shortcuts are too limited.
  • Consider DialogFragment when lifecycle handling matters.
  • Keep alert-dialog lists short and focused for the best user experience.

Course illustration
Course illustration

All Rights Reserved.