1

I am trying to make a popup that is going to have text fields and information to ask the user but I am wondering how to make it so that the user can close it by clicking outside of the popup where the main fragment / activity is.

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.tabstudygroups, container, false);

        listview = (ListView) rootView.findViewById(R.id.clist2);
        addCourseButton = (Button) rootView.findViewById(R.id.caddcoursebutton);

        // do stuff here

        addCourseButton.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onClick(View v) {
        if(v == addCourseButton) {
            View popupView = LayoutInflater.from(getActivity()).inflate(R.layout.popup_layout, null);
            final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);

            // HERE IS WHAT I THOUGHT WOULD MAKE IT BE ABLE TO ENABLE THE OUTSIDE TOUCH
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setOutsideTouchable(true);

            Button btn = (Button) popupView.findViewById(R.id.button);

            popupWindow.showAsDropDown(popupView, 0, 0);
        }
    }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
SQLUser
  • 97
  • 11
  • 1
    Possible duplicate of [PopupWindow - Dismiss when clicked outside](https://stackoverflow.com/questions/12232724/popupwindow-dismiss-when-clicked-outside) – John Joe Mar 26 '18 at 03:38
  • I gave you wrong answer please have a look here https://stackoverflow.com/questions/3121232/android-popup-window-dismissal – noman404 Mar 26 '18 at 03:56

7 Answers7

1

Normally you'd do this with a dialog and an OnCancelListener. If you want the flexibility of a popup though, you can get the same things by setting it outside touchable, then calling setTouchInterceptor to intercept touches. Remember to return false if the touch is inside the window, so it will go down the touch chain to the actual view.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
1

Make your PopupWindow to wrap_content and make it focusable.

final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);

            // HERE IS WHAT I THOUGHT WOULD MAKE IT BE ABLE TO ENABLE THE OUTSIDE TOUCH
            popupWindow.setOutsideTouchable(true);
            popupWindow.setFocusable(true);
            popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));


            Button btn = (Button) popupView.findViewById(R.id.button);

            popupWindow.showAsDropDown(popupView, 0, 0);
MJM
  • 5,119
  • 5
  • 27
  • 53
1

Make sure that popupWindow.showAsDropDown(popupView, 0, 0); is after these popupWindow.setOutsideTouchable(true); popupWindow.setFocusable(true); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

Jerome.T
  • 13
  • 1
  • 6
0

Have you tried setCanceledOnTouchOutside?

0

You can use setCanceledOnTouchOutside(true) to close popup when user touch outside of popup.

Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(true);
Jaimin Prajapati
  • 341
  • 5
  • 14
0

Let popup window setTouchable with true, and setTouchInterceptor and get return false, then you can click outside of popup window to close it.

popWindow.setTouchable(true);
popWindow.setTouchInterceptor(new View.OnTouchListener() {
    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }
});

If it cannot work, please tell me, and I will see what I miss.

This answer is similar to Gabe Sechan's answer, and I just not discovered before I post this answer...

Sean Lee
  • 73
  • 1
  • 2
  • 9
0

2023 UPDATE: It has now been changed to setCancelable.

dialog.setCancelable(true);

John Seong
  • 45
  • 1
  • 8