Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect touch events in Android

Tags:

android

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.

like image 470
ddavtian Avatar asked Jul 19 '10 03:07

ddavtian


People also ask

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 .

What is touch events in mobile application?

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.

Which class is used to determine the action of touch event?

Touch events are generated to track the actions of touch points. A touch event is represented by an instance of the TouchEvent class.

What is touch gestures in Android?

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.


2 Answers

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.

like image 80
theWook Avatar answered Sep 28 '22 22:09

theWook


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

like image 29
nicholas.hauschild Avatar answered Sep 29 '22 00:09

nicholas.hauschild