0

I'm creating a custom Dialog that contains a graphic, some text which I modify on the fly with setMessage() and a single button, labeled 'OK', which, when pressed, should dismiss the dialog and do some housekeeping. My code looks like this:

        // Shows the number of letters correct in the current guess.
    wdsBuilder = new AlertDialog.Builder(this);
    inflater = this.getLayoutInflater();
    dialogView = inflater.inflate(R.layout.box_dialog3, null);
    dialogView.setBackgroundColor(Color.TRANSPARENT);
    alertTextView = (TextView)dialogView.findViewById(R.id.text);

    wdsBuilder.setView(dialogView).setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int whichButton) {
            wdsAlert.dismiss();
            if (playTimer != null) playTimer.cancel();
        }
    });
    wdsAlert = wdsBuilder.create();

Very standard; in fact, I lifted it almost verbatim from https://developer.android.com/guide/topics/ui/dialogs.html. The dialog displays correctly and when I press 'OK' it gets dismissed, but when I put a breakpoint at wdsAlert.dismiss(), the breakpoint isn't hit. Anyone have a clue what's going on?

FractalBob
  • 3,225
  • 4
  • 29
  • 40

6 Answers6

1

you can write dialog.dismiss(); instead of wdsAlert.dismiss();

Hardik Mehta
  • 2,195
  • 1
  • 11
  • 14
0

Try this if you want to dismiss dialog on click-:

wdsBuilder.setView(dialogView).setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int whichButton) {
           dialog.dismiss();
        }
    });
Shivam Oberoi
  • 1,447
  • 1
  • 9
  • 18
0

You don't need to call dismiss() on button click . Its AlertDialog's default behavior. Use it as below and check.

 AlertDialog.Builder wdsBuilder = new AlertDialog.Builder(this);
    View dialogView = LayoutInflater.from(this).inflate(R.layout.item_dialog, null);
    dialogView.setBackgroundColor(Color.TRANSPARENT);
    TextView alertTextView = (TextView)dialogView.findViewById(R.id.text);
    wdsBuilder.setView(dialogView).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int whichButton) {
          Log.e("Dismiss","called");
        }
    });
    AlertDialog wdsAlert = wdsBuilder.create();
    wdsAlert.show();

And if you do not want to dismiss dialog on onClick of PositiveButton the you need to override its behavior. Have look into This thread.

ADM
  • 20,406
  • 11
  • 52
  • 83
0

Try to clean your project if the breackPoint doesnt work, but here is my Dialog that work for me:

 final Dialog dialogAnomalias;
            dialogAnomalias = new Dialog(MainActivity.this);
            dialogAnomalias.setContentView(R.layout.content_main);
            dialogAnomalias.setTitle("Captura");
            dialogAnomalias.setCancelable(false);

            WindowManager.LayoutParams lp = setDialogLayoutParams(dialogAnomalias);

            lecturaAnomalia = (EditText) dialogAnomalias.findViewById(R.id.etLectura);
            comentariosAnomalia = (EditText) dialogAnomalias.findViewById(R.id.etObservaciones);


            Button btnAceptar = (Button) dialogAnomalias.findViewById(R.id.btnAceptar);
            Button btnCancelar = (Button) dialogAnomalias.findViewById(R.id.btnCancelar);
            btnAceptar.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                }
            });
            btnCancelar.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    dialogAnomalias.dismiss();
                }
            });
            dialogAnomalias.show();
            dialogAnomalias.getWindow().setAttributes(lp);

and this method for the Dialog:

public WindowManager.LayoutParams setDialogLayoutParams(Dialog dialog) {
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(dialog.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.MATCH_PARENT;
    return lp;
}
Jorge Barraza Z
  • 171
  • 3
  • 9
0

Thanks for all your suggestions, but I found a workaround that doesn't require processing a click event.

And just now, I stumbled across the solution: elsewhere in the code, I had called setButton() on the builder and this was catching the button click, rather than the call to setPositiveButton().

FractalBob
  • 3,225
  • 4
  • 29
  • 40
0

You have to use

    wdsBuilder.setView(dialogView).setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { 

@Override public void onClick(DialogInterface dialog, int whichButton) { 

dialog.dismiss(); 

if (playTimer != null) playTimer.cancel(); 

} });

not

wdsBuilder.setView(dialogView).setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { 

    @Override public void onClick(DialogInterface dialog, int whichButton) { 

    wdsAlert.dismiss(); 

    if (playTimer != null) playTimer.cancel(); 

    } });

because you are passing it as parameter in onClick function....

If you are getting struck with such error use toasts or snack bars to track errors

saicharan
  • 1
  • 1
  • You don't understand. The problem is that the onClick() method never gets called, not that the dialog doesn't disappear. – FractalBob Dec 29 '17 at 08:09