0

I need help!

I'm developing an android app and I ran into a problem. I have an AlertDialog which contains two buttons(positive and negative). When a button is clicked certain code runs, then the dialog is closed.

dialog.setNegativeButton("button name", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // some code
        }
    });

But that's not what I want. When the users clicks the negative button I want some code to run and then the dialog SHOULDN'T be closed.

dialog.setNegativeButton("button name", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // some code
            // code to prevent the dialog from being closed ?
        }
    });

Is there anything I can do to prevent the dialog from being closed when the positive or negative button is clicked ?

I tried using this code:

dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(false);

But it doesn't work because now the user can't click the button.

BTW I'm developing for minimum sdk version of 16.

Thanks for helping!

tdxius
  • 281
  • 3
  • 11

3 Answers3

0

If the code in onClick contains dialog.dismiss() then only dialog would get closed

Calvin
  • 617
  • 1
  • 12
  • 34
0
   String Strmessage="message";
            final AlertDialog.Builder alt_bld = new AlertDialog.Builder(SplashActivity.this);
            alt_bld.setMessage(Strmessage);
            alt_bld.setCancelable(true);
            alt_bld.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            })
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            });
            AlertDialog alert = alt_bld.create();           
            alt_bld.setMessage(Strmessage);
            alert.show();

        }
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49
0

Thanks for the suggestions. I made a custom dialog and now it works just like i wanted.

tdxius
  • 281
  • 3
  • 11