55

Can I just don't dismiss my AlertDialog after clicking PositiveButton?

I would like to remain the dialog to show something update on my ArrayAdapter listWords.

This is my code.

AlertDialog.Builder sayWindows = new AlertDialog.Builder(MapActivity.this);

final EditText saySomething = new EditText(MapActivity.this);

sayWindows.setPositiveButton("ok",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    say = userName + " Says: "+saySomething.getText();
                    showPosition.setText(say);                      
                }
            });

sayWindows.setNegativeButton("cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

sayWindows.setAdapter(listWords, null);
sayWindows.setView(saySomething);
sayWindows.create().show();
ColdFire
  • 6,764
  • 6
  • 35
  • 51
Yi-Ying Lu
  • 565
  • 1
  • 4
  • 5
  • I put sayWindows.setCancelable(false) below setTitle, but it's not working. – Yi-Ying Lu Sep 28 '14 at 16:17
  • And it's not work in PositiveButton onClick either. – Yi-Ying Lu Sep 28 '14 at 16:20
  • 2
    Possible duplicate of [How to prevent a dialog from closing when a button is clicked](http://stackoverflow.com/questions/2620444/how-to-prevent-a-dialog-from-closing-when-a-button-is-clicked) – Suragch Nov 18 '16 at 00:31
  • [Disable the button until the user is ready to go on](http://stackoverflow.com/a/40669929/3681880) rather than blocking a dismiss. – Suragch Nov 18 '16 at 06:16

5 Answers5

80

After looking at @Little Child solution, I try to make this. Let us know if this works for you.

    AlertDialog.Builder sayWindows = new AlertDialog.Builder(
            MapActivity.this);
    final EditText saySomething = new EditText(MapActivity.this);
    sayWindows.setPositiveButton("ok", null);
    sayWindows.setNegativeButton("cancel", null);
    sayWindows.setAdapter(listWords, null);
    sayWindows.setView(saySomething);

    final AlertDialog mAlertDialog = sayWindows.create();
    mAlertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {

            Button b = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
            b.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    // TODO Do something
                   say = userName + " Says: "+saySomething.getText();
                   showPosition.setText(say); 
                }
            });
        }
    });
    mAlertDialog.show();
Chitrang
  • 5,097
  • 1
  • 35
  • 58
  • 1
    Thank you But the positive button seems no function. I put toast in there but it didn't work. – Yi-Ying Lu Sep 29 '14 at 01:02
  • 1
    Huff, finally did it, now its working, check it out. With changes of assigning AlertDialog with create, then setOnShowListener firt, and after that calling show(). Its working now, hopefully it works for you too. If it does, i request you to mark it as answer, so that in future others can find it easily. Thanks. – Chitrang Sep 29 '14 at 03:29
17

based on the most voted answer for How to prevent a dialog from closing when a button is clicked

final AlertDialog d = new AlertDialog.Builder(context)
            .setView(v)
            .setTitle(R.string.my_title)
            .setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick
            .setNegativeButton(android.R.string.cancel, null)
            .create();

    d.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {

            Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
            b.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    // TODO Do something

                }
            });
        }
    });  

I believe you need to override the positive button's handler. Add your logic to dismiss the dialog when a certain condition is met.

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221
14

Even Simpler:

final AlertDialog alertDialog = new AlertDialog.Builder(context).setView(v)
                .setPositiveButton(android.R.string.ok, null)
                .setNegativeButton(android.R.string.cancel, null)
                .show();

        Button b = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                //Do Your thing
            }
        });
M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69
9

Answer in Kotlin :

val dialog = AlertDialog.Builder(context)
    .setView(v)
    .setTitle(R.string.my_title)
    .setPositiveButton(android.R.string.ok, null)
    .setNegativeButton(android.R.string.cancel, null)
    .create()

dialog.setOnShowListener {

        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
            // Apply logic here
        }

    }
Steve Lukis
  • 420
  • 4
  • 10
  • Neither .setOnShowListener nor .getButton are available as public methods for AlertDialog.Builder (Kotlin 1.3.10). Am I missing something? – Bikeboy Jan 24 '19 at 16:16
  • These methods are contained in the AlertDialog. AlertDialog dialog = new AlertDialog.Builder(context)........create(); – Sergey Sereda Jun 07 '20 at 23:27
8

I do it like this:

    final AlertDialog dialog = new AlertDialog.Builder(this)
            .setCancelable(false)
            .setPositiveButton("YES", null)
            .setNegativeButton("NO", null)
            .show();

    Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
    positiveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
       //     Toast.makeText(SysManagerActivity.this, "dialog is open", Toast.LENGTH_SHORT).show();

        }
    });
Yury Matatov
  • 805
  • 3
  • 11
  • 23