Drag and Drop in Android

The Android System provides Drag and Drops framework, which facilities users’ especially to move data from one view to another within current layout. You can move your view from one layout to another.

The drag starts with a 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 in Eclipse. You need to target Android 4.0 or better.

2.      Add imageView in activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"





    android:padding="4sp" >






        android:src="@drawable/ic_launcher" />



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;



            protected void onCreate(Bundle savedInstanceState) {



                        imageView = (ImageView) findViewById(R.id.imageView);

                        myLayout = (ViewGroup) findViewById(R.id.relativeLayout);

                        // Call on touch listener





            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;




            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


                                    // Set coordinates value from Current location which changed after

                                    // touch or drag

                                    _xAxis = X - ParamsLayout.leftMargin;

                                    _yAxis = Y - ParamsLayout.topMargin;



                        case MotionEvent.ACTION_UP:


                        case MotionEvent.ACTION_POINTER_DOWN:

                        case MotionEvent.ACTION_POINTER_UP:


                        // A change has happened during a press gesture (between ACTION_DOWN and

                        // ACTION_UP).

                        case MotionEvent.ACTION_MOVE:

                                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view


                                    // ReSet coordinates after moves drag

                                    layoutParams.leftMargin = X - _xAxis;

                                    layoutParams.topMargin = Y - _yAxis;

                                    layoutParams.rightMargin = -50;

                                    layoutParams.bottomMargin = -50;





                        return true;





Now run your application

Running the Application

Hit on Run Button you see :


 Now drag and drop your image



Leave Comment