Android APIs
public class

GestureDetector

extends Object
java.lang.Object
   ↳ android.view.GestureDetector

Class Overview

Detects various gestures and events using the supplied MotionEvents. The GestureDetector.OnGestureListener callback will notify users when a particular motion event has occurred. This class should only be used with MotionEvents reported via touch (don't use for trackball events). To use this class:

Summary

Nested Classes
interface GestureDetector.OnDoubleTapListener The listener that is used to notify when a double-tap or a confirmed single-tap occur. 
interface GestureDetector.OnGestureListener The listener that is used to notify when gestures occur. 
class GestureDetector.SimpleOnGestureListener A convenience class to extend when you only want to listen for a subset of all the gestures. 
Public Constructors
GestureDetector(GestureDetector.OnGestureListener listener, Handler handler)
GestureDetector(GestureDetector.OnGestureListener listener)
This constructor was deprecated in API level 3. Use GestureDetector(android.content.Context, android.view.GestureDetector.OnGestureListener) instead.
GestureDetector(Context context, GestureDetector.OnGestureListener listener)
Creates a GestureDetector with the supplied listener.
GestureDetector(Context context, GestureDetector.OnGestureListener listener, Handler handler)
Creates a GestureDetector with the supplied listener that runs deferred events on the thread associated with the supplied Handler.
GestureDetector(Context context, GestureDetector.OnGestureListener listener, Handler handler, boolean unused)
Creates a GestureDetector with the supplied listener that runs deferred events on the thread associated with the supplied Handler.
Public Methods
boolean isLongpressEnabled()
boolean onTouchEvent(MotionEvent ev)
Analyzes the given motion event and if applicable triggers the appropriate callbacks on the GestureDetector.OnGestureListener supplied.
void setIsLongpressEnabled(boolean isLongpressEnabled)
Set whether longpress is enabled, if this is enabled when a user presses and holds down you get a longpress event and nothing further.
void setOnDoubleTapListener(GestureDetector.OnDoubleTapListener onDoubleTapListener)
Sets the listener which will be called for double-tap and related gestures.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public GestureDetector (GestureDetector.OnGestureListener listener, Handler handler)

Added in API level 1

This constructor was deprecated in API level 3.
Use GestureDetector(android.content.Context, android.view.GestureDetector.OnGestureListener, android.os.Handler) instead.

Creates a GestureDetector with the supplied listener. This variant of the constructor should be used from a non-UI thread (as it allows specifying the Handler).

Parameters
listener the listener invoked for all the callbacks, this must not be null.
handler the handler to use
Throws
NullPointerException if either listener or handler is null.

public GestureDetector (GestureDetector.OnGestureListener listener)

Added in API level 1

This constructor was deprecated in API level 3.
Use GestureDetector(android.content.Context, android.view.GestureDetector.OnGestureListener) instead.

Creates a GestureDetector with the supplied listener. You may only use this constructor from a UI thread (this is the usual situation).

Parameters
listener the listener invoked for all the callbacks, this must not be null.
Throws
NullPointerException if listener is null.
See Also

public GestureDetector (Context context, GestureDetector.OnGestureListener listener)

Added in API level 3

Creates a GestureDetector with the supplied listener. You may only use this constructor from a Looper thread.

Parameters
context the application's context
listener the listener invoked for all the callbacks, this must not be null.
Throws
NullPointerException if listener is null.
See Also

public GestureDetector (Context context, GestureDetector.OnGestureListener listener, Handler handler)

Added in API level 3

Creates a GestureDetector with the supplied listener that runs deferred events on the thread associated with the supplied Handler.

Parameters
context the application's context
listener the listener invoked for all the callbacks, this must not be null.
handler the handler to use for running deferred listener events.
Throws
NullPointerException if listener is null.
See Also

public GestureDetector (Context context, GestureDetector.OnGestureListener listener, Handler handler, boolean unused)

Added in API level 8

Creates a GestureDetector with the supplied listener that runs deferred events on the thread associated with the supplied Handler.

Parameters
context the application's context
listener the listener invoked for all the callbacks, this must not be null.
handler the handler to use for running deferred listener events.
unused currently not used.
Throws
NullPointerException if listener is null.
See Also

Public Methods

public boolean isLongpressEnabled ()

Added in API level 1

Returns
  • true if longpress is enabled, else false.

public boolean onTouchEvent (MotionEvent ev)

Added in API level 1

Analyzes the given motion event and if applicable triggers the appropriate callbacks on the GestureDetector.OnGestureListener supplied.

Parameters
ev The current motion event.
Returns

public void setIsLongpressEnabled (boolean isLongpressEnabled)

Added in API level 1

Set whether longpress is enabled, if this is enabled when a user presses and holds down you get a longpress event and nothing further. If it's disabled the user can press and hold down and then later moved their finger and you will get scroll events. By default longpress is enabled.

Parameters
isLongpressEnabled whether longpress should be enabled.

public void setOnDoubleTapListener (GestureDetector.OnDoubleTapListener onDoubleTapListener)

Added in API level 3

Sets the listener which will be called for double-tap and related gestures.

Parameters
onDoubleTapListener the listener invoked for all the callbacks, or null to stop listening for double-tap gestures.