Monday, June 18, 2012

How To Implement A Drag And Drop Application In Android

The project describes how to implement an application which allows you to drop and drag UI elements.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project DragNew
2.) Open and insert following in main.xml:
<LinearLayout xmlns:android=""


<ImageView android:id="@+id/img" android:layout_width="wrap_content"android:layout_height="wrap_content" android:src="@drawable/elmer" />
3.) Open and insert following in your strings.xml:
    <string name="hello">Drag and Drop, MyActivity!</string>
    <string name="app_name">Drag and Drop</string>
    <string name="drag">drag</string>
4.) Add some image in your drawable folder.
5.) Run the application. It allows you to drag and drop the element drawn on screen from one place to another.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. DragNew. Enter following information:
Project name: DragNew
Build Target: Android 2.3.1
Application name: DragNew
Package name: com.sample.dragdrop
Create Activity: MyActivity
On Clicking Finish DragNew code structure is generated with the necessary Android Packages being imported along with DragNew class will look like following:
package com.sample.dragdrop;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MyActivity extends Activity {
        private View selected_item = null;
        private int offset_x = 0;
        private int offset_y = 0;

    public void onCreate(Bundle savedInstanceState) {
        ViewGroup vg = (ViewGroup)findViewById(;
        vg.setOnTouchListener(new View.OnTouchListener() {
                        public boolean onTouch(View v, MotionEvent event) {
                                        case MotionEvent.ACTION_MOVE:
                                                int x = (int)event.getX() -offset_x;
                                                int y = (int)event.getY() -offset_y;
                        int w = getWindowManager().getDefaultDisplay().getWidth()- 100;
                        int h =getWindowManager().getDefaultDisplay().getHeight() - 100;
                        if(> w)
                            x = w;
                        if(> h)
                            y = h;
                                         LinearLayout.LayoutParams lp = newLinearLayout.LayoutParams(
                                        new ViewGroup.MarginLayoutParams(
                                         lp.setMargins(x, y, 0, 0);
                                return true;
       ImageView img = (ImageView)findViewById(;
        img.setOnTouchListener(new View.OnTouchListener() {
                        public boolean onTouch(View v, MotionEvent event) {
                                        case MotionEvent.ACTION_DOWN:
                                                offset_x = (int)event.getX();
                                                offset_y = (int)event.getY();
                                                selected_item = v;
                                return false;
Output – The final output

If you want to only move image and not view check this code DragDrop