Reputation: 3534
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?
Upvotes: 0
Views: 638
Reputation: 3534
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().
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation: 191
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;
}
Upvotes: 0
Reputation: 21043
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.
Upvotes: 0
Reputation: 1445
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();
}
});
Upvotes: 0
Reputation: 2425
you can write dialog.dismiss();
instead of wdsAlert.dismiss();
Upvotes: 1