Don’t Dismiss Popup On Outside Click Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Don’t Dismiss Popup On Outside Click without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have a popup window that I would like to inflate and dismiss programatically, I do not want any user input to dismiss it. However when the screen is touched outside of the pop up the pop up is dismissed, I know this question has been asked before but I’ve tried every variation I found and I can’t seem to make this work… Here is my code:

LayoutInflater inflater = (LayoutInflater)
                mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
        View popupView = inflater.inflate(R.layout.custom_loading, null);
        int width = LinearLayout.LayoutParams.WRAP_CONTENT;
        int height = LinearLayout.LayoutParams.WRAP_CONTENT;
        boolean focusable = false;
        popupWindow = new PopupWindow(popupView, width, height, focusable);
        popupWindow.setElevation(20);
        popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(mContext, R.drawable.transparent_back));
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchInterceptor((View view, MotionEvent motionEvent) -> {return false;});
        popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);// v is a view passed as a parameter to the function

Thanks in advance! 🙂

Answer

Fixed! The new code:

 boolean focusable = false;
 popupWindow.setBackgroundDrawable(null);

The following was deleted:

popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setTouchInterceptor((View view, MotionEvent motionEvent) -> {return false;});
We are here to answer your question about Don’t Dismiss Popup On Outside Click - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji