articles

Home / DeveloperSection / Articles / Drag and Drop Event in Android

Drag and Drop Event in Android

Manoj Pandey5447 12-Feb-2015

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"
    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

Running the Application

Hit on Run Button you see :

Drag and Drop Event in Android  

 Now drag and drop your image

   Drag and Drop Event in Android    

  Drag and Drop Event in Android



Updated 07-Sep-2019

Leave Comment

Comments

Liked By