Android Popup Window Dismissal

How to dismiss a popup in Android?

Here is the answer -

just write like this below:

popupWindow.setOutsideTouchable(true);
popupWindow.setCancelable(true);

it will work fine!
Please vote if answer is helpful.

PopupWindow - Dismiss when clicked outside

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

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 dismissal

This is because the popup window does not respond to onTouch or onKey events unless it has a background that != null. Check out some code I wrote to help with this. In the basic case you can to call PopupWindow#setBackgroundDrawable(new BitmapDrawable()) to force it to act the way you expect. You won't need your own onKey listener. You might also need to call PopupWindow#setOutsideTouchable(true) if you want it to go away when the user clicks outside of the window boundaries.

Extended esoteric answer:

The reason the background cannot be null is because of what happens in PopupWindow#preparePopup. If it detects background != null it creates an instance of PopupViewContainer and calls setBackgroundDrawable on that and puts your content view in it. PopupViewContainer is basically a FrameLayout that listens for touch events and the KeyEvent.KEYCODE_BACK event to dismiss the window. If background == null, it doesn't do any of that and just uses your content view. You can, as an alternative to depending on PopupWindow to handle that, extend your root ViewGroup to behave the way you want.



Related Topics



Leave a reply



Submit