0

I want to know how to close a pop_up window once the user clicks outside it, I had a look at PopupWindow - Dismiss when clicked outside but without any luck, and I tried that code:

pw.setBackgroundDrawable(null);
pw.setOutsideTouchable(true);
pw.setTouchInterceptor(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event)
          {
           if(event.getAction() == MotionEvent.ACTION_OUTSIDE)
                {
                    pw.dismiss();
                    return true;
                 }
            return false;
         }
     });
Community
  • 1
  • 1
MRefaat
  • 515
  • 2
  • 8
  • 22
  • I think you forget to add `@Override` before `onTouch(....)` method used. try this way `@Override public boolean onTouch(View v, MotionEvent event) { ........}` – M D Mar 13 '14 at 10:46

4 Answers4

1

Try this out.Hope it works :)

solution 1:

popupWindow.setFocusable(true);
popupWindow.update();

If this dont work.Then you can try this out.

solution 2:

You can use dialog.setCanceledOnTouchOutside(true); which will close the dialog if you touch outside of the dialog.

Something like,

Dialog dialog = new Dialog(context)
  dialog.setCanceledOnTouchOutside(true);

Or if your Dialog in non-model then,

1 - Set the flag-FLAG_NOT_TOUCH_MODAL for your dialog's window attribute

Window window = this.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

2 - Add another flag to windows properties,, FLAG_WATCH_OUTSIDE_TOUCH - this one is for dialog to receive touch event outside its visible region.

3 - Override onTouchEvent() of dialog and check for action type. if the action type is 'MotionEvent.ACTION_OUTSIDE' means, user is interacting outside the dialog region. So in this case, you can dimiss your dialog or decide what you wanted to perform. view plainprint?

public boolean onTouchEvent(MotionEvent event)  
{  

       if(event.getAction() == MotionEvent.ACTION_OUTSIDE){  
        System.out.println("TOuch outside the dialog ******************** ");  
               this.dismiss();  
       }  
       return false;  
}  
Harshal Benake
  • 2,391
  • 1
  • 23
  • 40
1

Ok so fixed in the end.

First made the main layout which the popup sits on a relative layout. Then placed a full screen blank layout on top which I made invisible and transparent.

Then show when the popup is shown, set the full screen panel visible with setVisibility(View.VISIBLE); and hide when popup is hidden with setVisibility(View.GONE);

Also need to return true from an on touch listener for the layout with (To stop touch events passing back to the main layout):

blocker.setOnTouchListener(new OnTouchListener() { 
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});

And give the popup window the properties:

setTouchable(true);
setOutsideTouchable(false);

Cheers

Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
  • This is an exact copy+paste of [this SO answer](http://stackoverflow.com/a/8398317/1682559)... And it is the opposite of what the OP wants.. – Kevin Cruijssen Aug 06 '14 at 07:16
0

you should'nt set the BackgroundDrawable to null, because that kills the OnTouchListener ; you should replace pw.setBackgroundDrawable(null); by pw.setBackgroundDrawable(new BitmapDrawable())

Houssem
  • 36
  • 2
0

Better use a dialog Fragment for the same. It is made for the popup functionality and closes by default on pressing outside the dialog Fragment or using the hard back button.

Kailas
  • 7,350
  • 3
  • 47
  • 63