Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Click Through PopupWindow

I have created a class that extends popupwindow. its constructor looks something like the following

super(builder.context.get());

this.setWindowLayoutMode(ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT);
setFocusable(true);
setBackgroundDrawable(new BitmapDrawable());
setTouchInterceptor(onTouchListener);
FrameLayout frameLayout = new FrameLayout(builder.context.get());
LayoutInflater inflater = (LayoutInflater) builder.context.get().getSystemService(
        Context.LAYOUT_INFLATER_SERVICE);
View overlayBase = inflater.inflate(R.layout.tutorial_view_items, null, false);
frameLayout.addView(builder.backgroundView);
frameLayout.addView(overlayBase);
setContentView(frameLayout);

the onTouchListener looks like the following:

private OnTouchListener onTouchListener = new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        Log.d(TAG, "Received");

        if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(radius, 2)) {
            Log.d(TAG, "bubbled through");
            return false;
        }
        return true;
    }
};

to actually display the popupwindow, I call SomePopupWindow.showAtLocation(SomeActivity.getWindow().getDecorView().getRootView(), Gravity.CENTER, 0, 0);

Visually represented, here is the product in question

blah

The touchListener is responding, but the input does not get sent to the underlying activity OR the popupwindow? (the button is unresponsive, where as if i were to remove the ontouchlistener, the button works fine).

update setting the touch listener to the frameLayout (i.e. content view of popupwindow in this case) instead of the popupwindow itself allows me to click on the buttons defined in the popup. still looking for way to delegate the touch event to the underlying activity/view

SOLUTION

Register a touch interceptor for the Popupwindow. If you want the activity to handle it, assuming you have a reference to the activity, call someActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter);, if you want the popupwindow to handle the touch even, call someView.dispatchTouchEvent(theMotionEventPassedToTouchInterceptor) to the popupwindow's content view, as set in the popupwindows setContentView(someView) method.

My method specifically looked like the following

private OnTouchListener onTouchListener = new OnTouchListener() {
@Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(
                radius, 2)) {
            activity.get().dispatchTouchEvent(event);
            return false;
        }
        frameLayout.dispatchTouchEvent(event);
        return true;
    }
};
like image 281
rperryng Avatar asked Feb 22 '14 04:02

rperryng


1 Answers

SOLUTION

Register a touch interceptor for the Popupwindow. If you want the activity to handle it, assuming you have a reference to the activity, call someActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter);, if you want the popupwindow to handle the touch even, call someView.dispatchTouchEvent(theMotionEventPassedToTouchInterceptor) to the popupwindow's content view, as set in the popupwindows setContentView(someView) method.

My method specifically looked like the following

private OnTouchListener onTouchListener = new OnTouchListener() {
@Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(
                radius, 2)) {
            activity.get().dispatchTouchEvent(event);
            return false;
        }
        frameLayout.dispatchTouchEvent(event);
        return true;
    }
};
like image 57
rperryng Avatar answered Nov 09 '22 18:11

rperryng