PopupWindow - Dismiss when clicked outside PopupWindow - Dismiss when clicked outside android android

PopupWindow - Dismiss when clicked outside


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


I found that none of the answers supplied worked for me, except WareNinja's comment on the accepted answer, and Marcin S.'s will probably also work. Here's the part that works for me:

myPopupWindow.setBackgroundDrawable(new BitmapDrawable());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.


I met the same issues, and fixed it as below codes. It works fine for me.

    // Closes the popup window when touch outside.    mPopupWindow.setOutsideTouchable(true);    mPopupWindow.setFocusable(true);    // Removes default background.    mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

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

Have fun@.@