Drag and drop in Xamarin.Android

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.Drag += View_Drag;
    
  2. Implement ACTION_DROP in onDrag()

    void View_Drag(object sender, View.DragEventArgs e)
    {
        if (e.Event.Action == DragAction.Drop)
        {
            e.Handled = true;
            // ...
        }
    }
    
  3. Grab URI from DragEvent

    e.Event.ClipData.GetItemAt(0).Uri;
    
  4. Request a permission to this URI

    RequestDragAndDropPermissions(e.Event);
    
  5. Make a query to ContentProvider to fetch the dropped item

    var metadata = ContentResolver.Query(uri, new [] { OpenableColumns.DisplayName, OpenableColumns.Size}, null, null, null))
    var file = ContentResolver.OpenFileDescriptor(uri, mode: "r");