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.
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:
- Kotlin - Drag-and-drop Sample
- Java - Drag-and-drop Sample
Source code overview
These snippets show the key steps for drag-and-drop.
Attach a dragListener to the view where you want to drop.
View.setOnDragListener();
Implement ACTION_DROP in onDrag()
public boolean onDrag(View v, DragEvent event) { switch (event.getAction()) { case DragEvent.ACTION_DROP: return true; } }
Grab URI from DragEvent
DragEvent.getClipData().getItemAt().getUri();
Request a permission to this URI
requestDragAndDropPermissions(DragEvent);
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)