Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fire an event when click occurs outside a dialog

I would like to know how to solve a problem I've got.

I have a Dialog which pops up in an activity. The Dialog doesn't cover the whole screen, so the buttons from the activity still show. I can easily close the dialog when there is a touch outside the dialog's bounds with dialog.setCanceledOnTouchOutside(true);

However what I want to do is fire an event if a click is outside the Dialog's bounds (e.g if someone touches a button on the main Activity, it should close the Dialog and fire that event at the same time).

like image 614
fizo07 Avatar asked Mar 01 '12 12:03

fizo07


1 Answers

When dialog.setCanceledOnTouchOutside(true); then you just override onCancel() like this:

dialog.setOnCancelListener(         new DialogInterface.OnCancelListener() {             @Override             public void onCancel(DialogInterface dialog) {                 //When you touch outside of dialog bounds,                  //the dialog gets canceled and this method executes.             }         } ); 

Type your code inside the onCancel() method so it runs when the dialog gets canceled.

like image 192
Will Neithan Avatar answered Nov 07 '22 15:11

Will Neithan