GestureDetector
  public
  
  
  
  class
  GestureDetector
  
    extends Object
  
  
  
  
  
  
| java.lang.Object | |
| ↳ | android.view.GestureDetector | 
Detects various gestures and events using the supplied MotionEvents.
 The 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:
 
- Create an instance of the GestureDetectorfor yourView
- In the View.onTouchEvent(MotionEvent)method ensure you callonTouchEvent(android.view.MotionEvent). The methods defined in your callback will be executed when the events occur.
- If listening for OnContextClickListener.onContextClick(MotionEvent)you must callonGenericMotionEvent(android.view.MotionEvent)inView.onGenericMotionEvent(MotionEvent).
Summary
| Nested classes | |
|---|---|
| 
        
        
        
        
        interface | GestureDetector.OnContextClickListenerThe listener that is used to notify when a context click occurs. | 
| 
        
        
        
        
        interface | GestureDetector.OnDoubleTapListenerThe listener that is used to notify when a double-tap or a confirmed single-tap occur. | 
| 
        
        
        
        
        interface | GestureDetector.OnGestureListenerThe listener that is used to notify when gestures occur. | 
| 
        
        
        
        
        class | GestureDetector.SimpleOnGestureListenerA convenience class to extend when you only want to listen for a subset of all the gestures. | 
| Public constructors | |
|---|---|
| 
      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  | |
| 
      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  | |
| 
      GestureDetector(GestureDetector.OnGestureListener listener)
      
      This constructor is deprecated.
    Use  | |
| 
      GestureDetector(GestureDetector.OnGestureListener listener, Handler handler)
      
      This constructor is deprecated.
    Use  | |
| Public methods | |
|---|---|
| 
        
        
        
        
        
        boolean | 
      isLongpressEnabled()
       | 
| 
        
        
        
        
        
        boolean | 
      onGenericMotionEvent(MotionEvent ev)
      Analyzes the given generic motion event and if applicable triggers the
 appropriate callbacks on the  | 
| 
        
        
        
        
        
        boolean | 
      onTouchEvent(MotionEvent ev)
      Analyzes the given motion event and if applicable triggers the
 appropriate callbacks on the  | 
| 
        
        
        
        
        
        void | 
      setContextClickListener(GestureDetector.OnContextClickListener onContextClickListener)
      Sets the listener which will be called for context clicks. | 
| 
        
        
        
        
        
        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. | 
| Inherited methods | |
|---|---|
Public constructors
GestureDetector
public GestureDetector (Context context, GestureDetector.OnGestureListener listener)
Creates a GestureDetector with the supplied listener.
 You may only use this constructor from a Looper thread.
| Parameters | |
|---|---|
| context | Context: AnActivityor aContextcreated fromContext.createWindowContext(int, Bundle)This value may benull. | 
| listener | GestureDetector.OnGestureListener: the listener invoked for all the callbacks, this must
 not be null. If the listener implements theOnDoubleTapListenerorOnContextClickListenerthen it will also be set as the listener for
 these callbacks (for example when using theSimpleOnGestureListener). | 
| Throws | |
|---|---|
| NullPointerException | if listeneris null. | 
See also:
GestureDetector
public 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.
| Parameters | |
|---|---|
| context | Context: AnActivityor aContextcreated fromContext.createWindowContext(int, Bundle)This value may benull. | 
| listener | GestureDetector.OnGestureListener: the listener invoked for all the callbacks, this must
 not be null. If the listener implements theOnDoubleTapListenerorOnContextClickListenerthen it will also be set as the listener for
 these callbacks (for example when using theSimpleOnGestureListener). | 
| handler | Handler: the handler to use for running deferred listener events.
 This value may benull. | 
| Throws | |
|---|---|
| NullPointerException | if listeneris null. | 
See also:
GestureDetector
public 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.
| Parameters | |
|---|---|
| context | Context: AnActivityor aContextcreated fromContext.createWindowContext(int, Bundle)This value may benull. | 
| listener | GestureDetector.OnGestureListener: the listener invoked for all the callbacks, this must
 not be null. | 
| handler | Handler: the handler to use for running deferred listener events.
 This value may benull. | 
| unused | boolean: currently not used. | 
| Throws | |
|---|---|
| NullPointerException | if listeneris null. | 
See also:
GestureDetector
public GestureDetector (GestureDetector.OnGestureListener listener)
      This constructor is deprecated.
    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 | GestureDetector.OnGestureListener: the listener invoked for all the callbacks, this must
 not be null. | 
| Throws | |
|---|---|
| NullPointerException | if listeneris null. | 
See also:
GestureDetector
public GestureDetector (GestureDetector.OnGestureListener listener, Handler handler)
      This constructor is deprecated.
    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 | GestureDetector.OnGestureListener: the listener invoked for all the callbacks, this must
 not be null. | 
| handler | Handler: the handler to use
 This value may benull. | 
| Throws | |
|---|---|
| NullPointerException | if listeneris null. | 
Public methods
isLongpressEnabled
public boolean isLongpressEnabled ()
| Returns | |
|---|---|
| boolean | true if longpress is enabled, else false. | 
onGenericMotionEvent
public boolean onGenericMotionEvent (MotionEvent ev)
Analyzes the given generic motion event and if applicable triggers the
 appropriate callbacks on the OnGestureListener supplied.
| Parameters | |
|---|---|
| ev | MotionEvent: The current motion event.
 This value cannot benull. | 
| Returns | |
|---|---|
| boolean | true if the OnGestureListenerconsumed the event,
              else false. | 
onTouchEvent
public boolean onTouchEvent (MotionEvent ev)
Analyzes the given motion event and if applicable triggers the
 appropriate callbacks on the OnGestureListener supplied.
| Parameters | |
|---|---|
| ev | MotionEvent: The current motion event.
 This value cannot benull. | 
| Returns | |
|---|---|
| boolean | true if the OnGestureListenerconsumed the event,
              else false. | 
setContextClickListener
public void setContextClickListener (GestureDetector.OnContextClickListener onContextClickListener)
Sets the listener which will be called for context clicks.
| Parameters | |
|---|---|
| onContextClickListener | GestureDetector.OnContextClickListener: the listener invoked for all the callbacks, or null to stop
            listening for context clicks. | 
setIsLongpressEnabled
public 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. 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 | boolean: whether longpress should be enabled. | 
setOnDoubleTapListener
public void setOnDoubleTapListener (GestureDetector.OnDoubleTapListener onDoubleTapListener)
Sets the listener which will be called for double-tap and related gestures.
| Parameters | |
|---|---|
| onDoubleTapListener | GestureDetector.OnDoubleTapListener: the listener invoked for all the callbacks, or
        null to stop listening for double-tap gestures. | 
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
