Drag and drop

Support for drag-and-drop ensures a great user experience using all the different input types supported by the Surface Duo.

This sample demonstrates how to implement drag-and drop in your Android app, following the Android drag and drop guidance.

sample app UI using drag-and-drop

Key value

Drag-and-drop lets a user easily share data between parts of an app, or between different apps on two screens.

Types of apps that may benefit from this pattern

  • Apps that contain text or images.

For more info see the drag-and-drop section of Introduction to dual-screen devices.

Try our sample

Get the drag-and-drop sample on GitHub:

Source code overview

These snippets show the key steps for drag-and-drop.

  1. Attach a dragListener to the view where you want to drop.

    View.setOnDragListener();
    
  2. Implement ACTION_DROP in onDrag()

    public boolean onDrag(View v, DragEvent event) {
        switch (event.getAction()) {
            case DragEvent.ACTION_DROP:
                return true;
        }
    }
    
  3. Grab URI from DragEvent

    DragEvent.getClipData().getItemAt().getUri();
    
  4. Request a permission to this URI

    requestDragAndDropPermissions(DragEvent);
    
  5. Make a query to ContentProvider to fetch the dropped item

    File Metadata : ContentResolver.query( uri, new String[]{OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE}, null, null, null))
    Actual file :  ContentResolver.openFile(uri)