Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Use Drag-and-Drop in Android Apps?

Improve Article
Save Article
  • Last Updated : 08 Sep, 2021
Improve Article
Save Article

You may utilize the Android drag/drop framework to enable your users to move data with a graphical drag and drop action. This can be from one View in your own program to another, or between your app and another when multi-window mode is enabled. A drag event class, drag listeners, and auxiliary methods and classes are all included in the framework.

Sample Use Cases for Drag & Drop

  1. Something that helps you drag a pointer on live data to get the color of the object or the thing
  2. In your mobile device’s launcher screen, where every program displays, we may effortlessly drag and drop app icons from one location to another.

Geek Tip: To notify the system that a drag event is being called, your application calls the startDrag() function. This technique also distributes data from one view to another.

Gestures Used in the Full Event

Drag and Drop framework in Android allows you to drag and drop one view to another, i.e. in an Activity, if you have two or more views, you may move the data of one view from one view to the other using the Android drag and drop framework. For example, if your Android activity contains two TextViews, one in one half of the screen and the other in the other, the contents from the first TextView may be dragged and dropped to the other TextView.

  1. Started: You begin by producing a ClipData and a ClipData. The item for the data that is being transferred. Provide metadata that is kept in a ClipDescription object within the ClipData as part of the ClipData object. You might wish to use null instead of a real object for a drag and drop action that does not reflect data transfer.
  2. Continuing: If the drag event listener returns true, the program can initiate the drag event. The drag event is in progress, which means it has begun but has not yet finished. For example, if you wish to drag a TextView called tv1 from position 1 to position 2, the intermediate states between the two are in the continuous state.
  3. Dropped: The dragged item is released within the bounding box of a View. The system sends a drag event with the action type ACTION DROP to the View object’s listener.
  4. Ended: When the user drops the item after dragging it and all the events connected with that drop state are called, the system sends a signal to the application that the drop action is complete and all the needed functions are performed. As a result, the system shows that the drag and drop event has ended.

Events Involving Drag

Refer to the table below to understand all the different events involving drag:

getAction() value

What does it mean?

ACTION_DRAG_ENTEREDWhen a drag shadow enters the bounding box of a View object, the drag event listener receives this event action type. When the drag shadow enters the bounding box, the listener receives the first event action type. If the listener wants to receive drag events for this action in the future, it must return a boolean true to the system.
ACTION_DRAG_LOCATIONDRAG LOCATION This event action type is received by the drag event listener of a View object when it gets an ACTION DRAG ENTERED event while the drag shadow is still within the bounding box of the View.
ACTION_DRAG_STARTEDThe drag event listener of a View object receives this event action type immediately after the application runs startDrag() and obtains a drag shadow.
ACTION_DRAG_EXITEDThis event action type is received by the drag event listener of a View object when it receives an ACTION DRAG ENTERED and at least one ACTION DRAG LOCATION event, and after the user has dragged the drag shadow outside the bounding box of the View.

When the user releases the drag shadow over the View object, the drag event listener on the View object receives this event action type. This action type is only delivered to the listener of a View object if the listener responded true in response to the ACTION DRAG STARTED drag event. If the user releases the drag shadow on a View whose listener is not registered, or if the user releases the drag shadow on anything that is not part of the current layout, this action type is not transmitted.

If the drop is properly processed, the listener is supposed to return boolean true. If not, it should return false.

ACTION_DRAG_ENDEDNotifies a View that the drag-and-drop action is complete.

Effects and Shadows in the Drags

The system shows an image that the user drags during a drag and drop operation. This graphic depicts the data being pulled in terms of data mobility. The picture illustrates some parts of the drag process for other procedures.

The picture is known as a drag shadow. You build it by declaring methods for a View.DragShadowBuilder object and then passing it to the system when you start a drag with startDrag (). The system uses the callback methods described in View.DragShadowBuilder to obtain a drag shadow as part of its answer to startDrag().


In this example, we will drag and drop a text view within a bounding region from one location to another.

Step #1: First, add the application’s layout File


<?xml version="1.0" encoding="utf-8"?>
            android:background="#0F9D58 ">
                android:text="GFG | First App "
                android:textColor="#0F9D58 "/>

Step #2: Working with the Kotlin File


package com.geeksforgeeks.sampleDrag
import android.os.Bundle
import android.util.Log
import android.defGeeksView.DragEvent
import android.defGeeksView.MotionEvent
import android.defGeeksView.DefGeeksView
import android.defGeeksView.DefGeeksViewGroup
import android.widget.LinearLayout
class MainActivity: AppCompatActivity(), DefGeeksView.OnTouchListener, DefGeeksView.DragHappeningListener {
    private val TAG =
    override fun onCreate(savedInstanceState: Bundle?) {
    private fun theDraggers() {
    override fun dragHappening(defGeeksView:DefGeeksView, dragEvent: DragEvent):Boolean {
        Log.d(TAG, "dragHappening: defGeeksView->$defGeeksView\n DragEvent$dragEvent")
        when (dragEvent.action) {
            DragEvent.ACTION_DRAG_ENDED -> {
                return true
            DragEvent.ACTION_DRAG_EXITED -> {
                return true
            DragEvent.ACTION_DRAG_ENTERED -> {
                return true
            DragEvent.ACTION_DRAG_STARTED -> {
                return true
            DragEvent.ACTION_DROP -> {
                val gfgTextDefGeeksView = dragEvent.localState as DefGeeksView
                val defaultText = gfgTextDefGeeksView.parent as DefGeeksViewGroup
                val container = defGeeksView as LinearLayout
                gfgTextDefGeeksView.x = dragEvent.x
                gfgTextDefGeeksView.y = dragEvent.y
                return true
            DragEvent.ACTION_DRAG_LOCATION -> {
                return true
            else -> return false
    override fun onTouch(defGeeksView:DefGeeksView, motionEvent: MotionEvent):Boolean {
        return if (motionEvent.action === MotionEvent.ACTION_DOWN) {
            val dragShadowBuilder = DefGeeksView.DragShadowBuilder(defGeeksView)
            defGeeksView.startDrag(null, dragShadowBuilder, defGeeksView, 10)
        } else {

Get the full code here.


Devices running Android 7.0 (API level 24) or higher enable multi-window mode, which allows users to drag and drop data from one program to another:

  1. The app that initially had the data is referred to as the source app. It’s here when the slog begins.
  2. The app that receives data is referred to as the target app. It’s the point at which the drag comes to an end.

We learned how to use the drag and drop functionality in our Android app in this article. We discovered a slew of events related to the drag event. Finally, we demonstrated the drag and drop feature. So, utilizing the aforementioned approach, you may create additional drag-and-drop applications in Android.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!