# DropHelper

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

*** ** * ** ***


```
public final class DropHelper
```

<br />

*** ** * ** ***

Helper class used to configure `https://developer.android.com/reference/android/view/View.html`s to receive data dropped by a drag and drop operation. Includes support for content insertion using an `https://developer.android.com/reference/androidx/core/view/OnReceiveContentListener`. Adds highlighting during the drag interaction to indicate to the user where the drop action can successfully take place.

To ensure that drop target highlighting and text data handling work correctly, all `https://developer.android.com/reference/android/widget/EditText.html` elements in the drop target view's descendant tree (that is, any `EditText` elements contained within the drop target) must be provided as arguments to a call to `https://developer.android.com/reference/androidx/draganddrop/DropHelper.Options.Builder#addInnerEditTexts(android.widget.EditText...)`. Otherwise, an `EditText` within the target will steal the focus during the drag and drop operation, possibly causing undesired highlighting behavior.

Also, if the user is dragging text data and URI data in the drag and drop `https://developer.android.com/reference/android/content/ClipData.html`, one of the `EditText` elements in the drop target is automatically chosen to handle the text data. See `https://developer.android.com/reference/androidx/draganddrop/DropHelper.Options.Builder#addInnerEditTexts(android.widget.EditText...)` for the order of precedence in selecting the `EditText` that handles the text data.

This helper attaches an `https://developer.android.com/reference/androidx/core/view/OnReceiveContentListener` to drop targets and configures drop targets to listen for drag and drop events (see `https://developer.android.com/reference/androidx/draganddrop/DropHelper#configureView(android.app.Activity,android.view.View,java.lang.String[],androidx.core.view.OnReceiveContentListener)`). Do not attach an `https://developer.android.com/reference/android/view/View.OnDragListener.html` or additional `OnReceiveContentLister` to drop targets when using `https://developer.android.com/reference/androidx/draganddrop/DropHelper`.

**Note:** This class requires Android API level 24 or higher.

| See also |
|---|---|
| `https://developer.android.com/guide/topics/ui/drag-drop` | [Drag and drop](https://developer.android.com/guide/topics/ui/drag-drop) |
| `https://developer.android.com/guide/topics/large-screens/multi-window-support#dnd` | [Multi-window support](https://developer.android.com/guide/topics/large-screens/multi-window-support#dnd) |

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/draganddrop/DropHelper.Options` Options for configuring drop targets specified by `https://developer.android.com/reference/androidx/draganddrop/DropHelper`. |
| `public final class https://developer.android.com/reference/androidx/draganddrop/DropHelper.Options.Builder` Builder for constructing a `https://developer.android.com/reference/androidx/draganddrop/DropHelper.Options` instance. |

| ### Public methods |
|---|---|
| `static void` | `https://developer.android.com/reference/androidx/draganddrop/DropHelper#configureView(android.app.Activity,android.view.View,java.lang.String[],androidx.core.view.OnReceiveContentListener)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/app/Activity.html activity, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/View.html dropTarget, @https://developer.android.com/reference/androidx/annotation/NonNull String[] mimeTypes, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/core/view/OnReceiveContentListener onReceiveContentListener )` Configures a `View` for drag and drop operations, including the highlighting that indicates the view is a drop target. |
| `static void` | `https://developer.android.com/reference/androidx/draganddrop/DropHelper#configureView(android.app.Activity,android.view.View,java.lang.String[],androidx.draganddrop.DropHelper.Options,androidx.core.view.OnReceiveContentListener)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/app/Activity.html activity, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/View.html dropTarget, @https://developer.android.com/reference/androidx/annotation/NonNull String[] mimeTypes, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/draganddrop/DropHelper.Options options, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/core/view/OnReceiveContentListener onReceiveContentListener )` Configures a `View` for drag and drop operations, including the highlighting that indicates the view is a drop target. |

## Public methods

### configureView

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

```
public static void configureView(
    @NonNull Activity activity,
    @NonNull View dropTarget,
    @NonNull String[] mimeTypes,
    @NonNull OnReceiveContentListener onReceiveContentListener
)
```

Configures a `View` for drag and drop operations, including the highlighting that indicates the view is a drop target. Sets a listener that enables the view to handle dropped data.

Same as `` `https://developer.android.com/reference/androidx/draganddrop/DropHelper#configureView(android.app.Activity,android.view.View,java.lang.String[],androidx.draganddrop.DropHelper.Options,androidx.core.view.OnReceiveContentListener)` `` but with default configuration options.

**Note:** If the drop target contains `https://developer.android.com/reference/android/widget/EditText.html` elements, you must use `https://developer.android.com/reference/androidx/draganddrop/DropHelper#configureView(android.app.Activity,android.view.View,java.lang.String[],androidx.draganddrop.DropHelper.Options,androidx.core.view.OnReceiveContentListener)`. The `Options` argument enables you to specify a list of the `EditText` elements (see `https://developer.android.com/reference/androidx/draganddrop/DropHelper.Options.Builder#addInnerEditTexts(android.widget.EditText...)`).

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/app/Activity.html activity` | The current `Activity` (used for URI permissions). |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/View.html dropTarget` | A `View` that accepts the drag and drop data. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull String[] mimeTypes` | The MIME types the drop target can accept from the dropped data. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/core/view/OnReceiveContentListener onReceiveContentListener` | A listener that handles the dropped data. |

### configureView

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

```
public static void configureView(
    @NonNull Activity activity,
    @NonNull View dropTarget,
    @NonNull String[] mimeTypes,
    @NonNull DropHelper.Options options,
    @NonNull OnReceiveContentListener onReceiveContentListener
)
```

Configures a `View` for drag and drop operations, including the highlighting that indicates the view is a drop target. Sets a listener that enables the view to handle dropped data.

If the drop target's view hierarchy contains any `EditText` elements, they all must be specified in `options` (see `https://developer.android.com/reference/androidx/draganddrop/DropHelper.Options.Builder#addInnerEditTexts(android.widget.EditText...)`).

View highlighting occurs for a drag action only if a MIME type in the `https://developer.android.com/reference/android/content/ClipDescription.html` matches a MIME type provided in `mimeTypes`; wildcards are allowed (for example, "image/\*"). A drop can be executed and passed on to the `OnReceiveContentListener` even if the MIME type is not matched.

See `https://developer.android.com/reference/androidx/draganddrop/DropHelper` for more information.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/app/Activity.html activity` | The current `Activity` (used for URI permissions). |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/View.html dropTarget` | A `View` that accepts the drag and drop data. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull String[] mimeTypes` | The MIME types the drop target can accept from the dropped data. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/draganddrop/DropHelper.Options options` | Configuration options for the drop target (see `https://developer.android.com/reference/androidx/draganddrop/DropHelper.Options`). |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/core/view/OnReceiveContentListener onReceiveContentListener` | A listener that handles the dropped data. |