Android Drag and Drop: Enhancing User Interaction

In this article, you will learn about implementing Android drag and drop. You will also discover the benefits of using this feature and best practices to ensure an optimal user experience.

Android Drag and Drop Framework supports drag-and-drop in many applications. It allows you to create intuitive and engaging user interfaces by allowing users to drag and drop items within or between apps. It is a widely used feature in both mobile and web applications.

The drag and drop functionality in Android is based on the following classes:

DragShadowBuilderThis class is used to create a visual representation of the data being dragged. It extends the View.DragShadowBuilder class and overrides the onDrawShadow() method to draw the shadow on the screen.
DragEventThis class represents a drag event that occurs during the drag and drop process. It contains information about the action, the current position of the drag event, and the view that the event is currently over.
OnDragListenerThis interface is used to handle drag events. It contains two methods which are onDrag() and onDragEvent(). The onDrag() method is called when the user starts to drag an item, and the onDragEvent() method is called when the drag event occurs.

Android Drag And Drop Process

Android drag and drop involves the following steps:

  1. The user initiates the drag operation by pressing and holding a view. During this event, the startDrag() method is invoked, which creates a DragShadowBuilder object that shows the dragged view visually.
  2. When the user moves the view, the system generates ACTION_DRAG_ENTERED events, indicating that the drag has entered a new area. The target area where the view can be dropped is typically highlighted by this event.
  3. In response to the user moving the view, the system generates ACTION_DRAG_LOCATION events, indicating the current position. In this event, the visual representation of the dragged view can be updated.
  4. The system generates an ACTION_DRAG_EXITED event when the user moves the view out of the target area.
  5. ACTION_DROP events are generated when the user releases the view over the target area, indicating that the view has been dropped. Using this event, you can update the application state based on the drop.
  6. When the drag operation has ended, the system generates an ACTION_DRAG_ENDED event. Using this event, any drag-associated resources can be cleaned up.

The DragEvent Class

In Android, DragEvent represents an event that occurs during drag-and-drop operations.

Information about a drag-and-drop operation is provided, including information about the action, the drag event position, and the data that is being dragged.


Here are the constants present in the DragEvent class:

ACTION_DRAG_STARTEDIntroduced when a drag operation is started for the first time. It usually sets up the drag image as well as any other visual feedback.
ACTION_DRAG_ENTEREDThis action is sent when a new view is entered by the drag operation. In most cases, it is used to highlight the view over which the data is being dragged.
ACTION_DRAG_LOCATIONSends this action when the drag operation moves over a view. Visual feedback is typically updated to reflect the drag’s current position.
ACTION_DRAG_EXITEDA drag operation leaves a view when this action is sent. Normally, this is used to remove any visual feedback that was added to the view when the operation was entered.
ACTION_DROPSends an action when data is dropped onto a view. It typically handles dropped data and cleans up any visual feedback added during the operation.
ACTION_DRAG_ENDEDAn indication that the drag-and-drop operation has been completed.

DragEvent Methods

Methods of the DragEvent class are examined below:

getAction()Returns the DragEvent action type.
getClipData()Returns the ClipData object associated with the drag event.
getX(), getY()These methods return the X and Y coordinates of the drag event relative to the View receiving the drag event.
getResult()Returns a boolean value indicating whether the drop was successful.
setResult()The drag-and-drop result can be set using this method.
getLocalState()Returns an Object representing the drag and drop operation’s local state.
setDragShadow()Use this method to set a custom DragShadow.
getText()Returns the text data associated with drag event.
getClipDescription()Returns the ClipDescription object associated with the drag event.
getDragFlags()Returns the flags associated with the drag event.

Listening Drag Events

We need to implement the OnDragListener interface in an Android app to listen for drag events.

Android provides the OnDragListener interface for detecting drag events on views.

OnDragListener listens for drag and drop events and is associated with the view receiving the data.

This interface provides two methods to implement:

onDrag()The onDrag() method is called when a drag event occurs. This method takes two parameters which  the view receiving the drag event and the DragEvent object containing the event’s details.
onDragExited()When a user leaves the bounds of a view that receives drag events, this method is called.

A brief example of using OnDragListener to implement drag-and-drop functionality in Android is shown below.

First, let’s create two views: one view that we want to drag and another one that will serve as our drop target.

For this example, we will be using two TextView objects. To make it easy to differentiate between the two views, we will add a background drawable to each of them.


android:text="Drag me"
android:background="@drawable/draggable_background" />

android:text="Drop here"
android:background="@drawable/drop_target_background" />


After creating the two views, we will implement the OnDragListener interface for the drop target view.

By doing so, we can handle the actual drop event in the onDrag() method.

To handle specific drag events, we can add code to the appropriate case block.

TextView draggableText = findViewById(;
TextView dropTarget = findViewById(;

dropTarget.setOnDragListener(new View.OnDragListener() {
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
// Code to handle drag started event
return true;

// Code to handle drag entered event
return true;

// Code to handle drag location event
return true;

// Code to handle drag exited event
return true;

case DragEvent.ACTION_DROP:
// Code to handle drop event
return true;

// Code to handle drag ended event
return true;

return false;

Now, we can make the draggable view actually draggable by implementing a View.OnLongClickListener for the draggable view.

Inside the onLongClick() method, we’ll start the drag operation by creating a View.DragShadowBuilder object to provide a visual representation of the dragged view and calling startDragAndDrop(). This method takes a ClipData object and a View.DragShadowBuilder object as parameters. The ClipData object holds the data to be dragged and the View.DragShadowBuilder object provides a visual representation of the dragged view during the drag operation.

draggableText.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDragAndDrop(null, shadowBuilder, null, 0);
return true;

Android Drag and Drop Best Practices

To ensure that the drag and drop functionality in your app is optimal, it is important to follow these best practices:

  • Provide visual feedback to the user during the drag and drop operation, such as a visual cue to indicate the drop target.
  • Ensure that the drag and drop feature is accessible to users with disabilities by providing alternative ways to interact with the UI.
  • Optimize the drag and drop feature for performance by minimizing unnecessary redraws and animations.
  • Maintain consistency in the drag and drop behavior across all views and ensure that the user can easily understand how to use the feature.


Some of the advantages of drag and drop in Android are:

Improved user experienceDrag and drop functionality can provide a more intuitive and engaging user experience, as it allows users to interact with the content on the screen in a natural way.
Easy manipulation of dataWith drag and drop, users can easily move items or data around on the screen, making it easier to organize, categorize, or delete items.
Increased productivityBy using drag and drop, users can complete tasks more quickly and efficiently, especially when dealing with large amounts of data.
Compatibility with touchscreensDrag and drop is well-suited for touchscreens, as it enables users to directly manipulate objects on the screen.

In this article, you learned how to implement drag and drop functionality in your Android app. You discovered the three main classes used in drag and drop: DragShadowBuilder, DragEvent, and OnDragListener. Additionally, we went through the steps involved in implementing drag and drop in an Android application.

We value your feedback.

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!

Leave a Reply

Your email address will not be published. Required fields are marked *