Drag and drop between views in Android

Drag and drop lets people move a card, file or media item by holding it and placing it somewhere else. In Android, the gesture looks simple, but the implementation must decide what moves: only a visual preview, an item within one screen, or content shared with another application.

The safest approach starts with the behaviour you need rather than the animation you want. For two panes in the same Activity, a shared ViewModel can hold the pending item and destination. For a true system drag, add ClipData, a drag shadow and DragEvent listeners. This guide combines both patterns and covers validation, accessibility and testing on Australian devices.

Compare the available approaches

“Between views” can describe controls in one Activity, destinations in different Fragments, or a handover to another app. These cases have different rules. Same-window transfers can rely on in-process state, while cross-app transfers need a MIME type, readable content and appropriate permissions.

Approach Best suited to Main limitation
Shared ViewModel Moving items between panes in one app Does not create a system drag
startDragAndDrop Native drag gestures on API 24 and later Requires payload and target handling
startDrag Compatibility with Android versions before API 24 Creates a separate, deprecated code path
ItemTouchHelper Reordering items within a RecyclerView Does not support unrelated drop destinations

For most boards, planners and media trays, use a shared ViewModel as the source of truth and treat the native drag as input. On a valid drop, call moveItem in the ViewModel; both source and destination observe it and update together. This avoids fragile assumptions about touch coordinates and provides a natural path for undoing an incorrect move.

Start the drag from the source

Give the source a long-press gesture so ordinary taps and scrolling continue to work. Put the stable item ID in both ClipData and the local drag state. The ID makes the destination independent of a particular adapter position, while ClipData describes the payload if the drag ever leaves the app.

binding.sourceCard.setOnLongClickListener { view ->
    val clip = ClipData.newPlainText("item_id", item.id)
    val shadow = View.DragShadowBuilder(view)

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        view.startDragAndDrop(clip, shadow, item.id, 0)
    } else {
        @Suppress("DEPRECATION")
        view.startDrag(clip, shadow, item.id, 0)
    }

    true
}

View.startDragAndDrop is available from Android 7.0, or API 24. The guarded startDrag branch preserves support for older devices. The drag shadow is generated from the source view, so it follows the existing visual design without requiring a duplicate layout. Android supplies standard long-press feedback, but you can add subtle haptic feedback if the item is difficult to identify.

Accept and validate the drop

The destination must explicitly accept the payload before it receives later drag events. Returning true from ACTION_DRAG_STARTED confirms that the MIME type is supported. Keep the dragged ID separately from the visual highlight, then commit the move only after ACTION_DROP.

private var draggedId: String? = null
private var isOverTarget = false

binding.dropZone.setOnDragListener { target, event ->
    when (event.action) {
        DragEvent.ACTION_DRAG_STARTED -> {
            draggedId = event.localState as? String
            event.clipDescription?.hasMimeType("text/plain") == true
        }

        DragEvent.ACTION_DRAG_ENTERED -> {
            isOverTarget = true
            target.alpha = 0.75f
            true
        }

        DragEvent.ACTION_DRAG_EXITED -> {
            isOverTarget = false
            target.alpha = 1f
            true
        }

        DragEvent.ACTION_DROP -> {
            val id = draggedId
                ?: event.clipData?.getItemAt(0)?.text?.toString()

            if (id != null && isOverTarget) {
                viewModel.moveItem(id, target.tag.toString())
            }
            true
        }

        DragEvent.ACTION_DRAG_ENDED -> {
            draggedId = null
            isOverTarget = false
            target.alpha = 1f
            true
        }

        else -> false
    }
}

Do not signal acceptance with colour alone. Use a border, icon and concise label so the destination remains obvious for people with low vision or when the screen is exposed to bright sunlight. The same need for clear state communication appears in references such as Lightning Roulette variants, where changing modes must remain distinguishable without relying on one visual cue.

For an app-to-app drag, avoid exposing sensitive identifiers in plain text. Publish approved content through a content provider, declare the relevant MIME type and apply the minimum permissions required by the receiving app.

Check the interaction under Australian conditions

Google Play is the main Android storefront in Australia, and the device mix ranges from premium Samsung and Google phones to more affordable models. Screen dimensions, Android versions and touch precision can differ considerably. Test at least one compact handset, one large phone or tablet and a foldable when the product supports multi-window or split-screen use.

Melbourne and Sydney commuters often interact with apps one-handed on crowded trains, while users in regional areas may encounter patchy mobile coverage. Copy can reflect local usage without becoming gimmicky: people may casually describe passing an item as “chucking it over”, while a command button should remain direct and say “Move to list”.

  • Treat a long press as a deliberate action and preserve quick scrolling.
  • Store stable item IDs rather than adapter positions.
  • Reject invalid destinations without changing the underlying model.
  • Provide a TalkBack action or overflow-menu alternative for every drag.
  • Test large fonts, rotation, dark mode and interrupted network conditions.
  • Record drop success and failure without logging sensitive item data.

Automated tests should cover the ViewModel transition and the listener’s acceptance rules, while physical-device checks should cover drag shadows, auto-scroll and conflicts with other gestures. If the same move operation appears in a web console, web acceptance checks can help verify the shared contract, but they do not replace native Android testing.

Put the pattern into your next release

Start with one narrow flow, such as moving a task between two lists in a project board. Define the valid destinations, keep the item ID in the ViewModel, expose clear drag feedback and provide an accessible menu action that performs the same operation.

Build the first vertical slice in the next sprint: one source, one valid destination, one shared state model and one accessible alternative. Test it on the oldest supported Android release, a modern phone and a tablet before expanding the pattern across other screens. Ship that first tested flow, then extend it with confidence.