简体   繁体   中英

How to dismiss a pop_up window when clicking outside?

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;
         }
     });

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;  
}  

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM