How to prevent a dialog from closing when a item is selected

android alertdialog prevent dismiss when click outside
disable close dialog android
android dialog set cancel touch outside
android dialog disable background click
android alertdialog touch outside listener
android make dialog unclosable
dialog not dismissing android
android studio dialog disable click outside

I have a dialog with items:

final String[] items = new String[] { "item1", "item2" };

AlertDialog.Builder adb = new AlertDialog.Builder(this).setTitle("Title");
adb.setItems(items, new OnClickListener() {

    public void onClick(DialogInterface d, int n) {
        Toast.makeText(MainActivity.this, items[n], Toast.LENGTH_LONG).show();

AlertDialog alert = adb.create();

When a item is selected, the Toast is shown and the dialog closes. How can I disable this? I want that the dialog still remains open after a item is selected.

You are using AlertDialog.Builder.setItems() method which dismisses a dialog when an item is clicked.

In order to display a list of items and not have the dialog dismissed, you should use one of the overloads of setSingleChoiceItems() method:

Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener. The list will have a check mark displayed to the right of the text for the checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a button will dismiss the dialog.

How to prevent a dialog from closing when a button is clicked, DialogPreference - override showDialog() If you wish to prevent a dialog box from closing when one of these buttons is pressed you must replace the common button handler for the actual view of the button. Because it is assigned in OnCreate(), you must replace it after the default OnCreate() implementation is called. I've got a single choice alert dialog working, but I can't figure out how to access to OK/Cancel event to return my selected item and close the alert. This site uses cookies for analytics, personalized content and ads.

Set the second parameter in the setItems method to null and attach an item listener to the listview that is part of the dialog:

var alertDialog: AlertDialog? = null
val builder = AlertDialog.Builder(context!!,
    .setItems(items.toTypedArray(), null)
    .setTitle("My Dialog Title")
    ) { dialog, id ->
    ) { dialog, id ->

alertDialog =
alertDialog.listView.setOnItemClickListener(object: AdapterView.OnItemClickListener {
    override fun onItemClick(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
        // Do something with item click.

How to prevent a dialog from closing when a button is clicked?, This example demonstrates how do I prevent a dialog from closing when Select your mobile device as an option and then check your mobile� Prevent a userform from closing when the user clicks the x-button by using VBA in Microsoft Excel In this article, we will show how to disable the x-button on the userform by using VBA. In this example, we have created a userform which can be activated by clicking the run button on the worksheet.

There are two methods I use. The first one is the easiest, The second one is the same as the above @AndroidDev's answer.

First One

builder.setItems(items, (dialogInterface, n) -> {
        if(n == 0){
            Toast.makeText(MainActivity.this, items[n], Toast.LENGTH_LONG).show();

Second One: (Same as the above one but here as the Java8 lambda)

AlertDialog dialog = builder.create();
    dialog.getListView().setOnItemClickListener((adapterView, subview, i, l)->{


How to prevent a dialog from closing when a button is clicked , How to prevent a dialog from closing when a button is clicked the Android AlertDialog unless Duration: 6:21 Posted: Oct 18, 2016 FileDialog.SelectedItems property (Office) 01/09/2019; 2 minutes to read +1; In this article. Gets a FileDialogSelectedItems collection. This collection contains a list of the paths of the files that a user selected from a file dialog box displayed by using the Show method of the FileDialog object.

Prevent dialog from closing when positiveButton is clicked and , Step 1 − Create a new project in Android Studio, Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the� Prevent it from ever happening by hitting the Shift five times rapidly to bring up that very dialog box. Click the "Disable this keyboard shortcut in Ease of Access Keyboard Settings" and uncheck

I clicked "prevent this page from creating additional dialogs." How , How to undo the error caused by clicking "OK" on the "prevent this page Active. Always Active. Save Settings. Allow All. Skip to main content uses dialog boxes to confirm the deletion of meeting items and categories (shown above). from creating additional dialogs, you can reset the session by closing� Please post the code and flag the statement with the error when trying to figure out what went wrong and how to fix it. Assuming that you are using Application

Dialogs, Dialogs disable all app functionality when they appear, and remain on screen until confirmed, Simple dialogs display a list of items that take immediate effect when selected. To close an alert dialog, one of its actions must be selected. When a form is displayed as a modal dialog box, clicking the Close button (the button with an X at the upper-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel.