Is it possible to detect all touch events in an Activity and capture it and then in return pass that pass event to another View?
For example:
Button 1 and Button 2. When Button 1 is pressed I want to capture that touch/click event and automatically pass that touch event to Button 2, basically with one touch/press you get the click generated and that same click is passed on to the second button automatically.
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 .
Touch Event PropagationThe DOWN touch event is passed to "View C" onTouchEvent and the boolean result of TRUE or FALSE determines if the action is captured. Because "View C" returns true and is handling the gesture, the event is not passed to "ViewGroup B"'s nor "ViewGroup A"'s onTouchEvent methods.
Touch events are generated to track the actions of touch points. A touch event is represented by an instance of the TouchEvent class.
A "touch gesture" occurs when a user places one or more fingers on the touch screen, and your application interprets that pattern of touches as a particular gesture. There are correspondingly two phases to gesture detection: Gather data about touch events.
take look this API description first.
boolean android.app.Activity.dispatchTouchEvent(MotionEvent ev)
public boolean dispatchTouchEvent (MotionEvent ev) Since: API Level 1 Called to process touch screen events. You can override this to intercept all touch screen events before they are dispatched to the window. Be sure to call this implementation for touch screen events that should be handled normally.
Parameters ev The touch screen event.
Returns boolean Return true if this event was consumed.
As you can see, you can intercept all touch events.
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
super.dispatchTouchEvent(ev);
if(btn1.onTouchEvent(ev)){
return btn2.onTouchEvent(ev);
}else{
return false;
}
}
These codes are what you are looking I think.
I imagine that you could take the TouchEvent from the button press, and make a call to the other button, passing in the TouchEvent, but I am not sure how safe that would be. (Android may bomb on you)
A safer solution would be to subclass Button, and use the Observer design pattern. You could register each buttons to listen for button presses of each other button, and then you would be able to safely pass the TouchEvent's between all of them.
If you are unfamiliar with the Observer design pattern, here is a link: http://en.wikipedia.org/wiki/Observer_pattern
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With