kaushal trivedi
kaushal trivedi

Reputation: 3443

How to make listener for setCanceledOnTouchOutside(true)

I am working in a project which opens a dialog with image when user selects an image. Now due to requirement I have to close the dialog when touched on outside of dialog so I set the dialog.setCanceledOnTouchOutside(true). But another requirement says that I should change the page as soon as user touches out side the dialog so I was trying to find out how to do that. But I have tried both onCancelListener and onDismissListener. None of them get called when we use dialog.setCanceledOnTouchOutside(true). to cancel dialog.

Upvotes: 0

Views: 1755

Answers (2)

Fenix Voltres
Fenix Voltres

Reputation: 3448

If you are using DialogFragment class, just override its onCancel() method:

@Override public void onCancel(DialogInterface dialog) {
    super.onCancel(dialog);

    ...
}

Upvotes: 3

Jstone05
Jstone05

Reputation: 61

It's my understanding that you can't add additional handling with setCanceledOnTouchOutside as an alternative you can override your onTouchEvent to do stuff when they touch outside the box using MotionEvent.ACTION_OUTSIDE like so

public boolean onTouchEvent(MotionEvent event)  
{  

   if(event.getAction() == MotionEvent.ACTION_OUTSIDE){  
           doStuff(); 
           this.dismiss();  
   }  
   return false;  
}  

Upvotes: 0

Related Questions