Home » Android » android – How to consume and dismiss the PopupWindow when clicked outside?

android – How to consume and dismiss the PopupWindow when clicked outside?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m currently designing simple tips popups using the PopupWindow class. I’m able to dismiss the popup when clicked outside of the content. However the touch events outside are consumed by the underlining layout. How can I consume the outside touches so that when clicked outside I still have the popup dismissed but there are no touch events being sent to the layout below my PopupWindow?

Current set properties:

mPopupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.popup_bg));
mPopupWindow.setOutsideTouchable(true);
How to&Answers:

Turns out there is a way to do it! After looking deep into PopupWindow and ListPopupWindow I discovered the option setFocusable(true); will do exactly what I needed!

Answer:

If you are using ListPopupWindow then setModal(true) will do the trick.