# GestureDetector

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Nested Classes](https://developer.android.com/reference/android/view/GestureDetector#nestedclasses)\|[Ctors](https://developer.android.com/reference/android/view/GestureDetector#pubctors)\|[Methods](https://developer.android.com/reference/android/view/GestureDetector#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/view/GestureDetector#inhmethods)  

# GestureDetector

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/view/GestureDetector "View this page in Kotlin")\|Java

`
public


class
GestureDetector
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


`

|---|------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.view.GestureDetector |

<br />

*** ** * ** ***

Detects various gestures and events using the supplied[MotionEvent](https://developer.android.com/reference/android/view/MotionEvent)s. The[OnGestureListener](https://developer.android.com/reference/android/view/GestureDetector.OnGestureListener)callback will notify users when a particular motion event has occurred. This class should only be used with[MotionEvent](https://developer.android.com/reference/android/view/MotionEvent)s reported via touch (don't use for trackball events). To use this class:

- Create an instance of the`GestureDetector`for your[View](https://developer.android.com/reference/android/view/View)
- In the[View.onTouchEvent(MotionEvent)](https://developer.android.com/reference/android/view/View#onTouchEvent(android.view.MotionEvent))method ensure you call[onTouchEvent(android.view.MotionEvent)](https://developer.android.com/reference/android/view/GestureDetector#onTouchEvent(android.view.MotionEvent)). The methods defined in your callback will be executed when the events occur.
- If listening for[OnContextClickListener.onContextClick(MotionEvent)](https://developer.android.com/reference/android/view/GestureDetector.OnContextClickListener#onContextClick(android.view.MotionEvent))you must call[onGenericMotionEvent(android.view.MotionEvent)](https://developer.android.com/reference/android/view/GestureDetector#onGenericMotionEvent(android.view.MotionEvent))in[View.onGenericMotionEvent(MotionEvent)](https://developer.android.com/reference/android/view/View#onGenericMotionEvent(android.view.MotionEvent)).

<br />

## Summary

|                                                                                                                 ### Nested classes                                                                                                                 ||
|--------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` interface` | [GestureDetector.OnContextClickListener](https://developer.android.com/reference/android/view/GestureDetector.OnContextClickListener) The listener that is used to notify when a context click occurs.                               |
| ` interface` | [GestureDetector.OnDoubleTapListener](https://developer.android.com/reference/android/view/GestureDetector.OnDoubleTapListener) The listener that is used to notify when a double-tap or a confirmed single-tap occur.               |
| ` interface` | [GestureDetector.OnGestureListener](https://developer.android.com/reference/android/view/GestureDetector.OnGestureListener) The listener that is used to notify when gestures occur.                                                 |
| ` class`     | [GestureDetector.SimpleOnGestureListener](https://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener) A convenience class to extend when you only want to listen for a subset of all the gestures. |

|                                                                                                                                                                                                                                                                                                                                                                      ### Public constructors                                                                                                                                                                                                                                                                                                                                                                       ||
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[GestureDetector](https://developer.android.com/reference/android/view/GestureDetector#GestureDetector(android.content.Context,%20android.view.GestureDetector.OnGestureListener))`(`[Context](https://developer.android.com/reference/android/content/Context)` context, `[GestureDetector.OnGestureListener](https://developer.android.com/reference/android/view/GestureDetector.OnGestureListener)` listener) ` Creates a GestureDetector with the supplied listener.                                                                                                                                                                                                                                                                                    |
| ` `[GestureDetector](https://developer.android.com/reference/android/view/GestureDetector#GestureDetector(android.content.Context,%20android.view.GestureDetector.OnGestureListener,%20android.os.Handler))`(`[Context](https://developer.android.com/reference/android/content/Context)` context, `[GestureDetector.OnGestureListener](https://developer.android.com/reference/android/view/GestureDetector.OnGestureListener)` listener, `[Handler](https://developer.android.com/reference/android/os/Handler)` handler) ` Creates a GestureDetector with the supplied listener that runs deferred events on the thread associated with the supplied[Handler](https://developer.android.com/reference/android/os/Handler).                                   |
| ` `[GestureDetector](https://developer.android.com/reference/android/view/GestureDetector#GestureDetector(android.content.Context,%20android.view.GestureDetector.OnGestureListener,%20android.os.Handler,%20boolean))`(`[Context](https://developer.android.com/reference/android/content/Context)` context, `[GestureDetector.OnGestureListener](https://developer.android.com/reference/android/view/GestureDetector.OnGestureListener)` listener, `[Handler](https://developer.android.com/reference/android/os/Handler)` handler, boolean unused) ` Creates a GestureDetector with the supplied listener that runs deferred events on the thread associated with the supplied[Handler](https://developer.android.com/reference/android/os/Handler).        |
| ` `[GestureDetector](https://developer.android.com/reference/android/view/GestureDetector#GestureDetector(android.view.GestureDetector.OnGestureListener))`(`[GestureDetector.OnGestureListener](https://developer.android.com/reference/android/view/GestureDetector.OnGestureListener)` listener) ` *This constructor is deprecated. Use[GestureDetector(android.content.Context, android.view.GestureDetector.OnGestureListener)](https://developer.android.com/reference/android/view/GestureDetector#GestureDetector(android.content.Context,%20android.view.GestureDetector.OnGestureListener))instead.*                                                                                                                                                  |
| ` `[GestureDetector](https://developer.android.com/reference/android/view/GestureDetector#GestureDetector(android.view.GestureDetector.OnGestureListener,%20android.os.Handler))`(`[GestureDetector.OnGestureListener](https://developer.android.com/reference/android/view/GestureDetector.OnGestureListener)` listener, `[Handler](https://developer.android.com/reference/android/os/Handler)` handler) ` *This constructor is deprecated. Use[GestureDetector(android.content.Context, android.view.GestureDetector.OnGestureListener, android.os.Handler)](https://developer.android.com/reference/android/view/GestureDetector#GestureDetector(android.content.Context,%20android.view.GestureDetector.OnGestureListener,%20android.os.Handler))instead.* |

|                                                                                                                                                                                                                             ### Public methods                                                                                                                                                                                                                              ||
|------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` boolean` | ` `[isLongpressEnabled](https://developer.android.com/reference/android/view/GestureDetector#isLongpressEnabled())`() `                                                                                                                                                                                                                                                                                                                                         |
| ` boolean` | ` `[onGenericMotionEvent](https://developer.android.com/reference/android/view/GestureDetector#onGenericMotionEvent(android.view.MotionEvent))`(`[MotionEvent](https://developer.android.com/reference/android/view/MotionEvent)` ev) ` Analyzes the given generic motion event and if applicable triggers the appropriate callbacks on the[OnGestureListener](https://developer.android.com/reference/android/view/GestureDetector.OnGestureListener)supplied. |
| ` boolean` | ` `[onTouchEvent](https://developer.android.com/reference/android/view/GestureDetector#onTouchEvent(android.view.MotionEvent))`(`[MotionEvent](https://developer.android.com/reference/android/view/MotionEvent)` ev) ` Analyzes the given motion event and if applicable triggers the appropriate callbacks on the[OnGestureListener](https://developer.android.com/reference/android/view/GestureDetector.OnGestureListener)supplied.                         |
| ` void`    | ` `[setContextClickListener](https://developer.android.com/reference/android/view/GestureDetector#setContextClickListener(android.view.GestureDetector.OnContextClickListener))`(`[GestureDetector.OnContextClickListener](https://developer.android.com/reference/android/view/GestureDetector.OnContextClickListener)` onContextClickListener) ` Sets the listener which will be called for context clicks.                                                   |
| ` void`    | ` `[setIsLongpressEnabled](https://developer.android.com/reference/android/view/GestureDetector#setIsLongpressEnabled(boolean))`(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](https://developer.android.com/reference/android/view/GestureDetector#setOnDoubleTapListener(android.view.GestureDetector.OnDoubleTapListener))`(`[GestureDetector.OnDoubleTapListener](https://developer.android.com/reference/android/view/GestureDetector.OnDoubleTapListener)` onDoubleTapListener) ` Sets the listener which will be called for double-tap and related gestures.                                                |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||

## Public constructors

### GestureDetector

Added in[API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public GestureDetector (Context context, 
                GestureDetector.OnGestureListener listener)
```

Creates a GestureDetector with the supplied listener. You may only use this constructor from a[Looper](https://developer.android.com/reference/android/os/Looper)thread.

<br />

|                                                                                                                                                                                                                                                                                                    Parameters                                                                                                                                                                                                                                                                                                    ||
|------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `context`  | `Context`: An[Activity](https://developer.android.com/reference/android/app/Activity)or a[Context](https://developer.android.com/reference/android/content/Context)created from[Context.createWindowContext(int, Bundle)](https://developer.android.com/reference/android/content/Context#createWindowContext(int,%20android.os.Bundle))This value may be`null`. <br />                                                                                                                                                                                                                              |
| `listener` | `GestureDetector.OnGestureListener`: the listener invoked for all the callbacks, this must not be null. If the listener implements the[OnDoubleTapListener](https://developer.android.com/reference/android/view/GestureDetector.OnDoubleTapListener)or[OnContextClickListener](https://developer.android.com/reference/android/view/GestureDetector.OnContextClickListener)then it will also be set as the listener for these callbacks (for example when using the[SimpleOnGestureListener](https://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener)). <br /> |

|                                                        Throws                                                        ||
|------------------------------------------------------------------------------------------------|----------------------|
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException) | if`listener`is null. |

**See also:**

- [Handler.Handler()](https://developer.android.com/reference/android/os/Handler#Handler())  

### GestureDetector

Added in[API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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](https://developer.android.com/reference/android/os/Handler).

<br />

|                                                                                                                                                                                                                                                                                                    Parameters                                                                                                                                                                                                                                                                                                    ||
|------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `context`  | `Context`: An[Activity](https://developer.android.com/reference/android/app/Activity)or a[Context](https://developer.android.com/reference/android/content/Context)created from[Context.createWindowContext(int, Bundle)](https://developer.android.com/reference/android/content/Context#createWindowContext(int,%20android.os.Bundle))This value may be`null`. <br />                                                                                                                                                                                                                              |
| `listener` | `GestureDetector.OnGestureListener`: the listener invoked for all the callbacks, this must not be null. If the listener implements the[OnDoubleTapListener](https://developer.android.com/reference/android/view/GestureDetector.OnDoubleTapListener)or[OnContextClickListener](https://developer.android.com/reference/android/view/GestureDetector.OnContextClickListener)then it will also be set as the listener for these callbacks (for example when using the[SimpleOnGestureListener](https://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener)). <br /> |
| `handler`  | `Handler`: the handler to use for running deferred listener events. This value may be`null`. <br />                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |

|                                                        Throws                                                        ||
|------------------------------------------------------------------------------------------------|----------------------|
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException) | if`listener`is null. |

**See also:**

- [Handler.Handler()](https://developer.android.com/reference/android/os/Handler#Handler())  

### GestureDetector

Added in[API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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](https://developer.android.com/reference/android/os/Handler).

<br />

|                                                                                                                                                                                     Parameters                                                                                                                                                                                      ||
|------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `context`  | `Context`: An[Activity](https://developer.android.com/reference/android/app/Activity)or a[Context](https://developer.android.com/reference/android/content/Context)created from[Context.createWindowContext(int, Bundle)](https://developer.android.com/reference/android/content/Context#createWindowContext(int,%20android.os.Bundle))This value may be`null`. <br /> |
| `listener` | `GestureDetector.OnGestureListener`: the listener invoked for all the callbacks, this must not be null. <br />                                                                                                                                                                                                                                                          |
| `handler`  | `Handler`: the handler to use for running deferred listener events. This value may be`null`. <br />                                                                                                                                                                                                                                                                     |
| `unused`   | `boolean`: currently not used. <br />                                                                                                                                                                                                                                                                                                                                   |

|                                                        Throws                                                        ||
|------------------------------------------------------------------------------------------------|----------------------|
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException) | if`listener`is null. |

**See also:**

- [Handler.Handler()](https://developer.android.com/reference/android/os/Handler#Handler())  

### GestureDetector

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public GestureDetector (GestureDetector.OnGestureListener listener)
```

<br />

**This constructor is deprecated.**   
Use[GestureDetector(android.content.Context, android.view.GestureDetector.OnGestureListener)](https://developer.android.com/reference/android/view/GestureDetector#GestureDetector(android.content.Context,%20android.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).

<br />

|                                                         Parameters                                                         ||
|------------|----------------------------------------------------------------------------------------------------------------|
| `listener` | `GestureDetector.OnGestureListener`: the listener invoked for all the callbacks, this must not be null. <br /> |

|                                                        Throws                                                        ||
|------------------------------------------------------------------------------------------------|----------------------|
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException) | if`listener`is null. |

**See also:**

- [Handler.Handler()](https://developer.android.com/reference/android/os/Handler#Handler())  

### GestureDetector

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public GestureDetector (GestureDetector.OnGestureListener listener, 
                Handler handler)
```

<br />

**This constructor is deprecated.**   
Use[GestureDetector(android.content.Context, android.view.GestureDetector.OnGestureListener, android.os.Handler)](https://developer.android.com/reference/android/view/GestureDetector#GestureDetector(android.content.Context,%20android.view.GestureDetector.OnGestureListener,%20android.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).

<br />

|                                                         Parameters                                                         ||
|------------|----------------------------------------------------------------------------------------------------------------|
| `listener` | `GestureDetector.OnGestureListener`: the listener invoked for all the callbacks, this must not be null. <br /> |
| `handler`  | `Handler`: the handler to use This value may be`null`. <br />                                                  |

|                                                        Throws                                                        ||
|------------------------------------------------------------------------------------------------|----------------------|
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException) | if`listener`is null. |

## Public methods

### isLongpressEnabled

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public boolean isLongpressEnabled ()
```

<br />

<br />

|                           Returns                           ||
|-----------|--------------------------------------------------|
| `boolean` | true if longpress is enabled, else false. <br /> |

### onGenericMotionEvent

Added in[API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public boolean onGenericMotionEvent (MotionEvent ev)
```

Analyzes the given generic motion event and if applicable triggers the appropriate callbacks on the[OnGestureListener](https://developer.android.com/reference/android/view/GestureDetector.OnGestureListener)supplied.

<br />

|                                    Parameters                                     ||
|------|-----------------------------------------------------------------------------|
| `ev` | `MotionEvent`: The current motion event. This value cannot be`null`. <br /> |

|                                                                                 Returns                                                                                 ||
|-----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `boolean` | true if the[OnGestureListener](https://developer.android.com/reference/android/view/GestureDetector.OnGestureListener)consumed the event, else false. <br /> |

### onTouchEvent

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public boolean onTouchEvent (MotionEvent ev)
```

Analyzes the given motion event and if applicable triggers the appropriate callbacks on the[OnGestureListener](https://developer.android.com/reference/android/view/GestureDetector.OnGestureListener)supplied.

<br />

|                                    Parameters                                     ||
|------|-----------------------------------------------------------------------------|
| `ev` | `MotionEvent`: The current motion event. This value cannot be`null`. <br /> |

|                                                                                 Returns                                                                                 ||
|-----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `boolean` | true if the[OnGestureListener](https://developer.android.com/reference/android/view/GestureDetector.OnGestureListener)consumed the event, else false. <br /> |

### setContextClickListener

Added in[API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void setContextClickListener (GestureDetector.OnContextClickListener onContextClickListener)
```

Sets the listener which will be called for context clicks.

<br />

|                                                                              Parameters                                                                              ||
|--------------------------|--------------------------------------------------------------------------------------------------------------------------------------------|
| `onContextClickListener` | `GestureDetector.OnContextClickListener`: the listener invoked for all the callbacks, or null to stop listening for context clicks. <br /> |

### setIsLongpressEnabled

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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.

<br />

|                                  Parameters                                  ||
|----------------------|--------------------------------------------------------|
| `isLongpressEnabled` | `boolean`: whether longpress should be enabled. <br /> |

### setOnDoubleTapListener

Added in[API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void setOnDoubleTapListener (GestureDetector.OnDoubleTapListener onDoubleTapListener)
```

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

<br />

|                                                                             Parameters                                                                              ||
|-----------------------|----------------------------------------------------------------------------------------------------------------------------------------------|
| `onDoubleTapListener` | `GestureDetector.OnDoubleTapListener`: the listener invoked for all the callbacks, or null to stop listening for double-tap gestures. <br /> |