views:

125

answers:

1

Hello

Actually I'm trying to implement an ontouchlistener into my android (1.5) application. therefore i implemented the "ontouchlistener" into the class, and then i put my code into the:

public boolean onTouchEvent (MotionEvent e){
 //code
}

The problem is, that if I am dragging over an (e.g.) Spinner or EditTextView than this Method isn't called. The only way to solve this which i figured out is to add an ontouchlistener to each of this views manually:

((Spinner)(findViewById(R.id.spinner1))).setOnTouchListener(tl);
((Spinner)(findViewById(R.id.spinner2))).setOnTouchListener(tl);

(tl is the ontouchlistener) So isn't there a way to catch the touch event before it gets to each of those views? thanks in advance

Ripei

A: 

What exactly do you want to do - do you want to:

  • Detect whole gestures in some Activity.
  • Detect touches only for a given View

If you want the former:

Take a look at this example: http://stackoverflow.com/questions/937313/android-basic-gesture-detection.

Basically, you redirect all registered touch events to a GestureDetector. It understands what exactly is the user input (single tap, double tap, scroll, etc.) and calls the corresponding callback in a SimpleOnGestureListener, which you should implement.

If you want the latter:

You can either:

  1. Override the View's onTouchEvent().
  2. Implement and hook onTouchListener (if you want to get the touch before it's dispatched to it).

If you want to implement onTouchListener, you'll be doing your work not in the onTouchEvent() method, but in the interface's onTouch() method.

Hope that helps.

Dimitar Dimitrov
yes i want the first point you mentioned. but i am searching for a way to solve this without having to add a gesturelistener/onclicklistener to each view i add to the activity. but it seems like this isn't posisble.
Ripei
No, actually the first approach I mentioned is exactly what you want - take a look at the examples in the link. Basically, you redirect all touches from your `Activity` to a `GestureDetector`. This means that all touches, that aren't internally handled in your views, are catched and scanned, so that you know if the user has double-tapped or scrolled. The `SimpleOnGestureListener` is registered to your `GestureDetector`, which is responsible for the whole `Activity`. You don't add listeners for each and every `View`.
Dimitar Dimitrov