Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: delegate touch event to underlaying view

I have the following hierarchy: Activity -> PopupWindow -> CustomView

My the PopupWindow itself is a square, but transparent, so you can see the Activity sitting in the background. The CustomView is a circle embedded inside the PopupWindow. alt text

What I have achieved so far is

  1. User clicks on green circle and I invoke "some stuff"
  2. User clicks outside of the PopupWindow and the touch event gets dispatched to the Activity.

The missing part is now, to dispatch any touch event that happens inside the PopupWindow but outside the CustomView (circle) to the Activity.

I already know how to sense when the touch is outside of my circle. I just have problems delegating it to the Activity.

In my CustomView I have the following in onTouch

if (radiusTouch > maxRadius) {
    return false;
}

In my PopupWindow I already setup the following, but it gets never called:

popup.setTouchInterceptor(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.i(TAG, "PopupWindow :: onTouch()");
        return false;
    }
});

Anything else I have to do to delegate the touch event all the way to the Activity?

like image 879
znq Avatar asked Sep 30 '10 16:09

znq


People also ask

Which method you should override to control your touch action in Android?

Android supports multiple pointers, e.g. fingers which are interacting with the screen. The base class for touch support is the MotionEvent class which is passed to Views via the onTouchEvent() method. you override the onTouchEvent() method.

How are gestures preferred than touch events?

 Gestures are scripted “solutions” that take advantage of these touch events.  So instead of tracking two touch points to determine if they're moving away or closer to one another in order to manipulate the size of a photo, you can just use GESTURE_ZOOM.

Which method you should override to control your touch action?

To make sure that each view correctly receives the touch events intended for it, override the onInterceptTouchEvent() method.

How are Android touch events delivered?

The touch event is passed in as a MotionEvent , which contains the x,y coordinates, time, type of event, and other information. The touch event is sent to the Window's superDispatchTouchEvent() . Window is an abstract class. The actual implementation is PhoneWindow .


2 Answers

Consider the FLAG_NOT_TOUCH_MODAL:

/** Window flag: Even when this window is focusable (its
  * {@link #FLAG_NOT_FOCUSABLE is not set), allow any pointer events
  * outside of the window to be sent to the windows behind it.  Otherwise
  * it will consume all pointer events itself, regardless of whether they
  * are inside of the window. */
public static final int FLAG_NOT_TOUCH_MODAL    = 0x00000020;

You can use this code to upate the window flags after the popup window was shown.

FrameLayout popupContainer = (FrameLayout)contentView.getParent();
WindowManager.LayoutParams p = (WindowManager.LayoutParams)popupContainer.getLayoutParams();
p.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
WindowManager windowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
windowManager.updateViewLayout(popupContainer, p);
like image 131
alex2k8 Avatar answered Sep 27 '22 17:09

alex2k8


Try setting these on your PopupWindow instance:

window.setBackgroundDrawable(new BitmapDrawable());

window.setOutsideTouchable(true);

and

window.setTouchable(true);

This is just a suggestion... I haven't tested it. Let me know if it works.

[Note: This is for the onTouch() to get called on touch events..]

like image 20
Kiran Parmar Avatar answered Sep 27 '22 17:09

Kiran Parmar