Popupwindow - Dismiss When Clicked Outside

PopupWindow - Dismiss when clicked outside

Please try to set setBackgroundDrawable on PopupWindow that should close the window if you touch outside of it.

How do I enable a popup to close when clicked outside of it?

Make your PopupWindow to wrap_content and make it focusable.

final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);

// HERE IS WHAT I THOUGHT WOULD MAKE IT BE ABLE TO ENABLE THE OUTSIDE TOUCH
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

Button btn = (Button) popupView.findViewById(R.id.button);

popupWindow.showAsDropDown(popupView, 0, 0);

Dismissing PopupWindow the way PopupMenu can be dismissed by clicking outside

You just need to set setBackgroundDrawable and setOutsideTouchable properties of PopupWindow it should close the window if you touch outside of it.

PopupWindow popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new ColorDrawable());
popupWindow.setOutsideTouchable(true);

How to dismiss PopupWindow with touch outside when its mFocusable set false?

it may work

// Removes default background.
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
myPopupWindow.setOutsideTouchable(true);

Alternatively:

myPopupWindow.setFocusable(true);

Not sure what the differences are, but the ListPopupWindow source code actually uses the latter when it's modality is set to true with setModal, so at least the Android developers consider this a viable approach, and it's only one line.

BTW, Don't use BitmapDrawable deprecated constructor, use this new ColorDrawable(android.R.color.transparent) to replace default background.

Happy Coding :)

Android Popup Window dismisses when clicked outside

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



Related Topics



Leave a reply



Submit