---
title: "Drag and Drop Event in Android"  
description: "Hi everyone i am here to explain about Drag and Drop events in Android"  
author: "Manoj Pandey"  
published: 2015-02-12  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1575/drag-and-drop-event-in-android  
category: "android"  
tags: ["android"]  
reading_time: 6 minutes  

---

# Drag and Drop Event in Android

The [Android](https://www.mindstick.com/articles/1567/navigation-drawer-implementation-in-android) System provides Drag and Drops [framework](https://yourviews.mindstick.com/view/81101/time-for-improvement-in-healthcare-facility-framework), which facilities users’ especially to move data from one view to another within current layout. You can move [your view](https://yourviews.mindstick.com/view/85403/what-is-your-view-about-84-lakh-yonis-in-hinduism) from one layout to another.\

The drag starts with a [gesture](https://www.mindstick.com/blog/11625/sending-valentine-flowers-is-a-beautiful-gesture), like when the user touches a view. A drag shadow is then created to represent the view that we want to drag.

In this example we will see how to drag and Image View in android on touch drag event.

1. Create a new [project](https://yourviews.mindstick.com/story/1788/things-to-ensure-your-construction-project-is-successful) in Eclipse. You need to target Android 4.0 or better.

2. Add [imageView](https://www.mindstick.com/forum/34664/how-to-set-an-image-on-imageview-in-android) in activity_main.xml

```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/relativeLayout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:padding="4sp" >    <ImageView        android:id="@+id/imageView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" /></RelativeLayout>
```

\
3. Now add following code in MainActivity

```
package com.example.dragexample;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.RelativeLayout;
 
// Implements your class with  View.OnTouchListener
public class MainActivity extends Activity implements View.OnTouchListener {            ImageView imageView;            private ViewGroup myLayout;            private int _xAxis;            private int _yAxis; 
            @Override            protected void onCreate(Bundle savedInstanceState) {                        super.onCreate(savedInstanceState);                        setContentView(R.layout.activity_main);                        imageView = (ImageView) findViewById(R.id.imageView);                        myLayout = (ViewGroup) findViewById(R.id.relativeLayout);                        // Call on touch listener                        imageView.setOnTouchListener(this);
            } 
            @Override            public boolean onCreateOptionsMenu(Menu menu) {                        // Inflate the menu; this adds items to the action bar if it is present.
                        getMenuInflater().inflate(R.menu.main, menu);
                        return true;
            } 
            @Override            public boolean onTouch(View view, MotionEvent event) {
                        // MotionEvent will sometimes return absolute X and Y coordinates
                        // relative to the view, and sometimes relative coordinates to the
                        // previous motion even
                        final int X = (int) event.getRawX();
                        final int Y = (int) event.getRawY();
                         switch (event.getAction() & MotionEvent.ACTION_MASK) {
                        // A pressed gesture has started, the motion contains the initial
                        // starting location.
                        case MotionEvent.ACTION_DOWN:
                                    RelativeLayout.LayoutParams ParamsLayout = (RelativeLayout.LayoutParams) view
                                                            .getLayoutParams();
                                    // Set coordinates value from Current location which changed after
                                    // touch or drag
                                   _xAxis = X - ParamsLayout.leftMargin;
                                    _yAxis = Y - ParamsLayout.topMargin;
                                     break;
                        case MotionEvent.ACTION_UP:
                                    break;
                        case MotionEvent.ACTION_POINTER_DOWN:
                        case MotionEvent.ACTION_POINTER_UP:
                                    break;
                        // A change has happened during a press gesture (between ACTION_DOWN and
                        // ACTION_UP).
                        case MotionEvent.ACTION_MOVE:
                                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                                                            .getLayoutParams();
                                   // ReSet coordinates after moves drag
                                    layoutParams.leftMargin = X - _xAxis;
                                    layoutParams.topMargin = Y - _yAxis;
                                    layoutParams.rightMargin = -50;
                                    layoutParams.bottomMargin = -50;
                                    view.setLayoutParams(layoutParams);
                                    break;
                        }
                        myLayout.invalidate();
                        return true;
            }
 
}
```

Now run [your application](https://answers.mindstick.com/qa/95487/how-do-you-troubleshoot-when-your-application-link-is-not-responding)

Running the Application

Hit on Run Button you see :

![Drag and Drop Event in Android](https://www.mindstick.com/mindstickarticle/4a95add3-7267-4092-8960-94be29d04305/images/ae5e07c9-6fab-4ad1-a04b-431424d54827.png)

Now drag and drop your image

![Drag and Drop Event in Android](https://www.mindstick.com/mindstickarticle/4a95add3-7267-4092-8960-94be29d04305/images/aa2981fa-c822-4432-ab46-e39ed996ad7c.png)

![Drag and Drop Event in Android](https://www.mindstick.com/mindstickarticle/4a95add3-7267-4092-8960-94be29d04305/images/1d58c6af-02d3-44a2-a67d-4c19cf9549a9.png)\

\

---

Original Source: https://www.mindstick.com/articles/1575/drag-and-drop-event-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
