Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding PopupWindow dismissal after touching outside

Tags:

android

I would like to use a PopupWindow with following behaviours/features:

  • It is focusable (has interactive controls inside eg. buttons)
  • The View 'under' popupwindow has to consume the touches outside the popup properly
  • .. but the popupwindow has to stay on screen even after clicking outside

I've found bunch of posts regarding PopupWindow but none of them asked question how to deal with such situation..

I think I tried every possible combination of setOutsideTouchable(), setFocusable(),setTouchable() but I'm stuck. Popup deals with clicks on it properly, but it's dismissed always when touching outside.

My current code is :

View.OnTouchListener customPopUpTouchListenr = new View.OnTouchListener(){

    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        Log.d("POPUP", "Touch false");
        return false;
    }

};


LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout= (LinearLayout)inflater.inflate(R.layout.insert_point_dialog, null);
PopupWindow pw = new PopupWindow(layout,400,200,true);
pw.setOutsideTouchable(true);
pw.setTouchable(true);
pw.setBackgroundDrawable(new BitmapDrawable());
pw.setTouchInterceptor(customPopUpTouchListenr);
pw.showAtLocation(frameLayout, Gravity.BOTTOM, 0, 0);

My general goal is to create a floating window which behaves like a 'tools palette' in software like gimp: has some controls inside, stays on top until closed by 'X' button, and allowing to interact with controls outside-under it.. Maybe there's some better way to do this, not a PopupWindow? But I still haven't found more suitable control.

like image 290
Michał Wróbel Avatar asked May 02 '12 00:05

Michał Wróbel


1 Answers

Nothing suggested here, or elsewhere seemed to work for me. So I did this:

popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getX() < 0 || motionEvent.getX() > viewWidth) return true;
                if (motionEvent.getY() < 0 || motionEvent.getY() > viewHight) return true;

                return false;
            }
        });

If the touch is within the bounds of the popupWIndow, the touch event not consumed (so buttons or scrollviews will work). If the touch is outside the bounds, the touch is consumed and not passed to the popupWindow so it is NOT dismissed.

like image 122
Larry Avatar answered Sep 18 '22 17:09

Larry