# DragStartHelper

# DragStartHelper

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core)  
[View Source](https://cs.android.com/search?q=file:androidx/core/view/DragStartHelper.java+class:androidx.core.view.DragStartHelper)  
Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

*** ** * ** ***

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


```
class DragStartHelper
```

<br />

*** ** * ** ***

DragStartHelper is a utility class for implementing drag and drop support.

It detects gestures commonly used to start drag (long click for any input source, click and drag for mouse).

It also keeps track of the screen location where the drag started, and helps determining the hot spot position for a drag shadow.

Implement [DragStartHelper.OnDragStartListener](https://developer.android.com/reference/kotlin/androidx/core/view/DragStartHelper.OnDragStartListener) to start the drag operation:  

```kotlin
DragStartHelper.OnDragStartListener listener = new DragStartHelper.OnDragStartListener {
    protected void onDragStart(View view, DragStartHelper helper) {
        View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view) {
            public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
                super.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
                helper.getTouchPosition(shadowTouchPoint);
            }
        };
        view.startDrag(mClipData, shadowBuilder, mLocalState, mDragFlags);
    }
};
mDragStartHelper = new DragStartHelper(mDraggableView, listener);
```
Once created, DragStartHelper can be attached to a view (this will replace existing long click and touch listeners):  

```kotlin
mDragStartHelper.attach();
```
It may also be used in combination with existing listeners:  

```kotlin
public boolean onTouch(View view, MotionEvent event) {
    if (mDragStartHelper.onTouch(view, event)) {
        return true;
    }
    return handleTouchEvent(view, event);
}
public boolean onLongClick(View view) {
    if (mDragStartHelper.onLongClick(view)) {
        return true;
    }
    return handleLongClickEvent(view);
}
```

## Summary

|                                                                                                                 ### Nested types                                                                                                                  |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `interface `[DragStartHelper.OnDragStartListener](https://developer.android.com/reference/kotlin/androidx/core/view/DragStartHelper.OnDragStartListener) Interface definition for a callback to be invoked when a drag start gesture is detected. |

|                                                                                                                                                                                                                                             ### Public constructors                                                                                                                                                                                                                                             |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [DragStartHelper](https://developer.android.com/reference/kotlin/androidx/core/view/DragStartHelper#DragStartHelper(android.view.View,androidx.core.view.DragStartHelper.OnDragStartListener))`(` ` view: `[View](https://developer.android.com/reference/android/view/View.html)`,` ` listener: `[DragStartHelper.OnDragStartListener](https://developer.android.com/reference/kotlin/androidx/core/view/DragStartHelper.OnDragStartListener) `)` Create a DragStartHelper associated with the specified view. |

|                                ### Public functions                                 |
|-------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [attach](https://developer.android.com/reference/kotlin/androidx/core/view/DragStartHelper#attach())`()` Attach the helper to the view.                                                                                                                                                                                                              |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [detach](https://developer.android.com/reference/kotlin/androidx/core/view/DragStartHelper#detach())`()` Detach the helper from the view.                                                                                                                                                                                                            |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [getTouchPosition](https://developer.android.com/reference/kotlin/androidx/core/view/DragStartHelper#getTouchPosition(android.graphics.Point))`(point: `[Point](https://developer.android.com/reference/android/graphics/Point.html)`)` Compute the position of the touch event that started the drag operation.                                     |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | [onLongClick](https://developer.android.com/reference/kotlin/androidx/core/view/DragStartHelper#onLongClick(android.view.View))`(v: `[View](https://developer.android.com/reference/android/view/View.html)`)` Handle a long click event.                                                                                                            |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | [onTouch](https://developer.android.com/reference/kotlin/androidx/core/view/DragStartHelper#onTouch(android.view.View,android.view.MotionEvent))`(v: `[View](https://developer.android.com/reference/android/view/View.html)`, event: `[MotionEvent](https://developer.android.com/reference/android/view/MotionEvent.html)`)` Handle a touch event. |

## Public constructors

### DragStartHelper

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
DragStartHelper(
    view: View,
    listener: DragStartHelper.OnDragStartListener
)
```

Create a DragStartHelper associated with the specified view. The newly created helper is not initially attached to the view, [attach](https://developer.android.com/reference/kotlin/androidx/core/view/DragStartHelper#attach()) must be called explicitly.  

|                                                                        Parameters                                                                        |
|----------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------|
| `view: `[View](https://developer.android.com/reference/android/view/View.html)                                                                           | A View                        |
| `listener: `[DragStartHelper.OnDragStartListener](https://developer.android.com/reference/kotlin/androidx/core/view/DragStartHelper.OnDragStartListener) | listener for the drag events. |

## Public functions

### attach

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
fun attach(): Unit
```

Attach the helper to the view.

This will replace previously existing touch and long click listeners.  

### detach

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
fun detach(): Unit
```

Detach the helper from the view.

This will reset touch and long click listeners to `null`.  

### getTouchPosition

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
fun getTouchPosition(point: Point): Unit
```

Compute the position of the touch event that started the drag operation.  

|                                      Parameters                                       |
|---------------------------------------------------------------------------------------|------------------------------------------------------------------|
| `point: `[Point](https://developer.android.com/reference/android/graphics/Point.html) | The position of the touch event that started the drag operation. |

### onLongClick

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
fun onLongClick(v: View): Boolean
```

Handle a long click event.  

|                                 Parameters                                  |
|-----------------------------------------------------------------------------|-------------------------------------|
| `v: `[View](https://developer.android.com/reference/android/view/View.html) | The view that was clicked and held. |

|                                       Returns                                       |
|-------------------------------------------------------------------------------------|----------------------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | true if the callback consumed the long click, false otherwise. |

### onTouch

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
fun onTouch(v: View, event: MotionEvent): Boolean
```

Handle a touch event.  

|                                          Parameters                                           |
|-----------------------------------------------------------------------------------------------|---------------------------------------------------------------------|
| `v: `[View](https://developer.android.com/reference/android/view/View.html)                   | The view the touch event has been dispatched to.                    |
| `event: `[MotionEvent](https://developer.android.com/reference/android/view/MotionEvent.html) | The MotionEvent object containing full information about the event. |

|                                       Returns                                       |
|-------------------------------------------------------------------------------------|---------------------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | True if the listener has consumed the event, false otherwise. |