Search code examples
androidandroid-popupwindow

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


I have a PopupWindow and it is setFocusable(false), I want to dismiss it when touch outside, and it is already setOutsideTouchable(true), but with the setFocusable(false) it doesn't work, how can I solve this? Thanks in advance.

All the properties being set to my PopupWindow are as follows:

popUpWindow.setBackgroundDrawable(xx);
popUpWindow.setTouchable(true);
popUpWindow.setFocusable(false);
popUpWindow.setOutsideTouchable(true);
popUpWindow.setAnimationStyle(android.R.style.Animation_Dialog);
popUpWindow.update();

update:

I was wrong, the PopupWindow dismissed, but some other code make it show.


Solution

  • 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 :)