Android Drag and Drop

Android Drag and Drop

Android drag and drop construction allows users to go data in one View to some other View in current layout by using a graphical drag and drop gesture. By API 11 drag and drop of view onto other views or view groupings is backed.The system contains three steps (given below)  components to aid android drag and drop functionality

  • Android Drag event class:
  • Android Drag listeners:
  • Android Helper methods and classes:

android drag and drop

Android Drag and Drop Process

There are quite simply four steps in the android drag and drop procedure:

Started  – This event occurs when you begin dragging something in a design, your application message or calls startDrag() solution to tell the machine/framework or system to get started on a target. The quarrels inside startDrag() method supply the data to be dragged, metadata because of this data, and a callback for sketching the drag shadow.

The functional system first responds by phoning back again to your request to obtain a drag shadow. After that it displays the drag shadow on these devices.

Next, the machine sends a drag event with action type ACTION_Move_STARTED to the signed up drag event listeners for all your View objects in current layout.

To continue to get drag situations, including a possible drop event, a move event listener must gain true, When the drag event listener profits false, then you won’t receive drag occurrences for the existing procedure before system transmits a drag event with action type ACTION_DRAG_ENDED.

Continuing – the drag is extended by An individual. System sends ACTION_DRAG_ENTERED action accompanied by ACTION_DRAG_LOCATION  to the drag event listener just for the View where dragging point enters. The listener might want to modify its View object’s appearance in response to the function or can respond by highlighting its View.

The drag event listener gets a ACTION_Move_EXITED action following the user has migrated the drag shadow beyond your bounding pack of the View.

Dropped – An individual emits the dragged item within the bounding package of an View. The machine delivers the View object’s listener a drag event with action type ACTION_DROP.

Ended – Soon after the action type ACTION_DROP, the system delivers out a drag event with action type ACTION_Move_ENDED to point that the pull procedure is over

The DragEvent Class

The DragEvent presents an event that is delivered by the framework at various times throughout a drag and drop procedure. This category provides few Constants and important methods which we use during drag and drop process.


Following are constants integers available as the right part of DragEvent category.


Constants and Description


Indicators the beginning of a android drop and drag procedure.


Indicators to a View that the drag point has came into the bounding field of the View.


Delivered to a View after ACTION_Move_ENTERED if the drag shadow continues to be within the View object’s bounding pack.


Indicators that the drag has been migrated by an individual shadow beyond your bounding package of the View.


Indicators to a View that an individual has released the drag shadow, and the drag point is at the bounding container of the View.


Signals or Indicator to a View that the drag and drop procedure has concluded.

Android Drag and Drop Methods

There are different android drag and drop methods but Pursuing are few important & most commonly used methods available as part of DragEvent class.


Constants and Description

int getAction()

Examine the action value of the event..

ClipData getClipData()

Returns the ClipData thing sent to the device within the call to startDrag().

ClipDescription getClipDescription()

Results the ClipDescription thing within the ClipData.

boolean getResult()

Results a sign of the consequence of the drag and drop procedure.

float getX()

Provides the X coordinate of the drag point.

float getY()

Has got the Y coordinate of the drag point.

String toString()

Results a string representation of the DragEvent object.

Listening for Drag Event

If you’d like all of your views inside a Layout should answer Drag event in that case your view either implements View.OnDragListener or set up onDragEvent(DragEvent) callback method. Once the functional system calling the technique or listener, it moves to them a DragEvent thing explained above. You could have both a listener and a callback way for View subject. If this occurs, the machine first calls the listener and then described callback so long as listener profits true.

The blend of the onDragEvent(DragEvent) method and View.OnDragListener is analogous to the combo of the onTouchEvent() and View.OnTouchListener used in combination with touch events in old editions of Android.

Starting a Drag Event

You focus on making a ClipData and ClipData.Item for the info/data being moved. Within the ClipData object, source metadata that is stored in a ClipDescription subject within the ClipData. For an android drag and drop procedure that does not represent data movement, you may want to use null of an actual object instead.

Next either you can stretch expand View.DragShadowBuilder to make a drag shadow for dragging the view or just you may use View.DragShadowBuilder(View) to make a default drag shadow that is the same size as the View discussion transferred to it, with the touch point focused in the drag shadow.

Android Drag and Drop Example

Pursuing android drag and drop example shows the features of a straightforward Move & Drop using View.setOnLongClickListener(), View.setOnTouchListener()andView.OnDragEventListener().

Steps and Description

  1. You use Android studio IDE to build an Android software and name it as My Software under a package deal com.example.sappal.myapplication. While creating this task, ensure you Aim for SDK and Compile With at the latest version of Google android SDK to make use of higher levels of APIs.
  2. Modify src/ and add the code to determine event listeners and a call back options for the logo design image found in the example.
  3. Duplicate image abc.png in res/drawable-* folders. You should use images with different image resolution in the event you want to provide them for different devices.
  4. Modify design XML record res/structure/activity_main.xml to establish default view of the custom logo images.
  5. Run the application form to launch  emulator and validate the consequence of the changes done in the application form.

Following is the content of the modified main activity file src/ This file can include each of the fundamental android life-cycle methods.

package com.example.sappal.myapplication;

import android.content.ClipData;
import android.content.ClipDescription;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {
   ImageView img;
   String msg;
   private android.widget.RelativeLayout.LayoutParams layoutParams;
   protected void onCreate(Bundle savedInstanceState) {
      img.setOnLongClickListener(new View.OnLongClickListener() {
         public boolean onLongClick(View v) {
            ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());
            String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
            ClipData dragData = new ClipData(v.getTag().toString(),mimeTypes, item);
            View.DragShadowBuilder myShadow = new View.DragShadowBuilder(img);
            return true;
      img.setOnDragListener(new View.OnDragListener() {
         public boolean onDrag(View v, DragEvent event) {
               case DragEvent.ACTION_DRAG_STARTED:
               layoutParams = (RelativeLayout.LayoutParams)v.getLayoutParams();
               Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED");
               // Do nothing
               case DragEvent.ACTION_DRAG_ENTERED:
               Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED");
               int x_cord = (int) event.getX();
               int y_cord = (int) event.getY();
               case DragEvent.ACTION_DRAG_EXITED :
               Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED");
               x_cord = (int) event.getX();
               y_cord = (int) event.getY();
               layoutParams.leftMargin = x_cord;
               layoutParams.topMargin = y_cord;
               case DragEvent.ACTION_DRAG_LOCATION  :
               Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION");
               x_cord = (int) event.getX();
               y_cord = (int) event.getY();
               case DragEvent.ACTION_DRAG_ENDED   :
               Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENDED");
               // Do nothing
               case DragEvent.ACTION_DROP:
               Log.d(msg, "ACTION_DROP event");
               // Do nothing
               default: break;
            return true;
      img.setOnTouchListener(new View.OnTouchListener() {
         public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
               ClipData data = ClipData.newPlainText("", "");
               View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(img);
               img.startDrag(data, shadowBuilder, img, 0);
               return true;
               return false;
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(, menu);
      return true;
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
      //noinspection SimplifiableIfStatement
      if (id == {
         return true;
      return super.onOptionsItemSelected(item);


Following will be the content of res/layout/activity_main.xml file

<RelativeLayout xmlns:android=""
      android:text="Drag and Drop Example"
      android:textSize="30dp" />
      android:text="Andro Development"
      android:textColor="#ff14be3c" />>
      android:layout_alignStart="@+id/textView2" />



Following will be the content of res/values/strings.xml to define two new constants

<?xml version="1.0" encoding="utf-8"?>
   <string name="app_name">My Application</string>
   <string name="action_settings">Settings</string>


Following is the default content of AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
   android:versionName="1.0" >
      android:targetSdkVersion="22" />
      android:theme="@style/AppTheme" >
         android:label="@string/app_name" >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />


How about we attempt to run our adjusted My Application application we simply developed. I accept you had made your AVD while doing android environment setup. To run the application from Android studio, open one of your project activity file and snap  Android Studio Run Icon    from the tool bar. Android Studio installs the application on your AVD and begins it and if all is well with your set-up and application, it will show taking after Emulator window

android drag and drop

Click long on the displayed AndroDevelopment website logo and you’ll see that brand image moves just a little after 1 a few moments long click from its place, its the right time when you should start dragging the image. You may drag it across the drop and display it at a fresh location.

android drag and drop