Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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(  1000,  50,  WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,  WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |  WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,  PixelFormat.OPAQUE);  params.gravity = Gravity.BOTTOM; WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); wm.addView(v1, params); 
like image 209
MindWire Avatar asked Dec 08 '11 15:12

MindWire


People also ask

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

1.1. You can react to touch events in your custom views and your activities. 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 do I turn off Touch listener on Android?

Use btn. setEnabled(false) to temporarily disable it and then btn. setEnabled(true) to enable it again.

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?

Touch events are delivered first to Activity. dispatchTouchEvent. It's where you may catch them first. Here they get dispatched to Window, where they traverse View hierarchy, in such order that Widgets that are drawn last (on top of other widgets) have chance to process touch in View.


1 Answers

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() {     @Override     public boolean onTouch(View v, MotionEvent event) {         return true;     } }); 
like image 159
John Giotta Avatar answered Nov 03 '22 00:11

John Giotta