view raw
MindWire MindWire - 10 months ago 66
Android Question

Android: How to prevent any touch events from being passed from a view to the one underneath it?

Specifically using the code below, is there a way to modify it so that the activity under this newly created view does not receive any gestures?

View v1 = new View(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |

params.gravity = Gravity.BOTTOM;
WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
wm.addView(v1, params);


Add an onTouchEvent method to the view with top position then return true. True will tell the event bubbling that the event was consumed therefore prevent event from bubbling to other views.

protected boolean onTouchEvent (MotionEvent me) {
    return true;

For v1 you would do an import:

import android.view.View.OnTouchListener;

Then set the onTouchListener:

v1.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        return true;