Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView in AlertDialog

Tags:

android

I am using a ListView in an AlertDialog to display a list of items. When the user clicks on one of the items, I want the dialog to close. I would not have any action buttons on the dialog. Any ideas on how I would accomplish this?

like image 420
bugzy Avatar asked Mar 07 '10 17:03

bugzy


People also ask

How do I show a list in alerts?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.

How can I display a list view in an Android alert dialog?

Navigate to the app > res > layout and create a new layout file. Add a ListView as shown below. This layout would be displayed inside the AlertDialog.

Is AlertDialog deprecated?

A simple dialog containing an DatePicker . This class was deprecated in API level 26.


1 Answers

You should be able to do something like:

final CharSequence[] items = {"Foo", "Bar", "Baz"};  AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Make your selection"); builder.setItems(items, new DialogInterface.OnClickListener() {     public void onClick(DialogInterface dialog, int item) {          // Do something with the selection     } }); AlertDialog alert = builder.create(); alert.show(); 

This page has some other examples of different types of Dialogs.

like image 150
Erich Douglass Avatar answered Sep 18 '22 22:09

Erich Douglass