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?
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.
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.
A simple dialog containing an DatePicker . This class was deprecated in API level 26.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With