2

I'm trying to create a kind of popupMenuItem with a popupWindow and a custom layout.

I have a button which shows the popupWindow when I click on it. And I would like to trigger and event to dismiss this popupWindow when I click on this button one more time or when I click outside of the popupWindow.

But at the moment it doesn't work, my setTouchInterceptor is not triggered, have you got an idea to solve this problem ?

Each time this popupWindow is open, I can't have access to all the others UI elements even with a setOutsideTouchable(true).

Here my code:

  • popupMenuImageView is my dropdown button (imageview actually) which shows my popup window
  • popupWindow my global variable for my PopupWindod object.

    popupMenuImageView = (ImageView) getView().findViewById(R.id.popup_menu_imageView);
    
    popupMenuImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    
            if (popupWindow == null) {
                int width = 375;
                int height = 240;
    
                TableLayout tableLayout = (TableLayout) getView().findViewById(R.id.custom_popup_menu_id);
                LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View layout = layoutInflater.inflate(R.layout.custom_popup_menu, tableLayout);
    
                popupWindow = new PopupWindow(layout, width, height, true);
                popupWindow.setOutsideTouchable(true);
                popupWindow.setFocusable(true);
                popupWindow.setTouchInterceptor(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
                            popupWindow.dismiss();
                            return true;
                        }
                        return false;
                    }
                });
    
            }
            popupWindow.showAsDropDown(popupMenuImageView);
        }
    });
    

Also I've tried this which didn't work too:

popupMenuImageView = (ImageView) getView().findViewById(R.id.popup_menu_imageView);
createMenuItem();
popupMenuImageView.setOnClickListener(eventOpenCloseMenuItem);

View.OnClickListener eventOpenCloseMenuItem = new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            if (isShowing) {
                popupWindow.showAsDropDown(popupMenuImageView);
                isShowing = false;
            }else{
                popupWindow.dismiss();
            }
        }
    };

private void createMenuItem(){

        if (popupWindow == null) {
            int width = 375;
            int height = 240;

            TableLayout tableLayout = (TableLayout) getView().findViewById(R.id.custom_popup_menu_id);
            LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = layoutInflater.inflate(R.layout.custom_popup_menu, tableLayout);

            popupWindow = new PopupWindow(layout, width, height, true);
            popupWindow.setOutsideTouchable(false);
            popupWindow.setFocusable(true);
        }
Alex DG
  • 1,859
  • 2
  • 22
  • 34

2 Answers2

2

If you just want to dismiss the popup window by clicking outside of it you don't need to set interceptor. Just set background drawable to null e.g

popupWindow.setBackgroundDrawable (new BitmapDrawable());
popupWindow.setOutsideTouchable(true);

and remove:

popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
                    popupWindow.dismiss();
                    return true;
                }
                return false;
            }
        });

Please check my answer to similar question here PopupWindow - Dismiss when clicked outside

Community
  • 1
  • 1
Marcin S.
  • 11,161
  • 6
  • 50
  • 63
1

I believe you need to setBackgroundDrawable() before displaying the popup in order for it to handle touch events:

popupWindow.setBackgroundDrawable(new ColorDrawable());
cziemba
  • 664
  • 5
  • 10