Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the PopupWindow get notified of touched outside events?

Tags:

android

The documentation states that PopupWindow will be informed of touch events outside of its window when setOutsideTouchable(boolean touchable) is set to true. How is popupwindow informed? I don't see any listener like setOnOutsideTouchListener etc to receive that information.

Example

PopupWindow popup = new PopupWindow();
popup.setOutsideTouchable(true);
//now what..how to receive those touch outside events?

Thanks.

like image 656
Thupten Avatar asked Oct 19 '14 23:10

Thupten


2 Answers

try to use setTouchInterceptor like the following snipt of code

popup.setTouchInterceptor(new OnTouchListener() 
    {

        public boolean onTouch(View v, MotionEvent event) 
        {
            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) 
            {
                popup.dismiss();
                return true;
            }

            return false;
        }
    });

also don't forget to set thew following flag:

popup.setOutsideTouchable(true);
like image 124
moh.sukhni Avatar answered Nov 12 '22 14:11

moh.sukhni


When you touch outside the PopupWindow, OnDismissListener is triggered as touching outside window dismisses the window by default, so you can set OnDismissListener on popupWindow to listen to touch outside the window.

popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
    @Override
    public void onDismiss() {
         //Do Something here
    }
});
like image 40
Siddarth G Avatar answered Nov 12 '22 13:11

Siddarth G