SQLUser
SQLUser

Reputation: 97

How do I enable a popup to close when clicked outside of it?

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);
        }
    }
}

Upvotes: 1

Views: 2495

Answers (7)

John Seong
John Seong

Reputation: 105

2023 UPDATE: It has now been changed to setCancelable.

dialog.setCancelable(true);

Upvotes: 0

Jerome.T
Jerome.T

Reputation: 13

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

Upvotes: 1

Sean Lee
Sean Lee

Reputation: 73

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...

Upvotes: 0

Jaimin Prajapati
Jaimin Prajapati

Reputation: 341

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

Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(true);

Upvotes: 0

user9064724
user9064724

Reputation:

Have you tried setCanceledOnTouchOutside?

Upvotes: 0

MJM
MJM

Reputation: 5291

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);

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93542

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.

Upvotes: 1

Related Questions