And Drag
And Drag
Android drag/drop framework allows your users to move data from one View to another View in the
current layout using a graphical drag and drop gesture. As of API 11 drag and drop of view onto other
views or view groups is supported. The framework includes following three important components to
support drag & drop functionality −
Drag event class.
Drag listeners.
Helper methods and classes.
Constants
Following are all constants integers available as a part of DragEvent class.
Sr.No. Constants & Description
ACTION_DRAG_STARTED
1
Signals the start of a drag and drop operation.
ACTION_DRAG_ENTERED
2
Signals to a View that the drag point has entered the bounding box of
1
BCA 502, Mobile Application Development Batch 2021-2024
Dr Chandani Sharma Unit 3
M.M. Institute of Computer Technology & Business Management
the View.
ACTION_DRAG_LOCATION
3 Sent to a View after ACTION_DRAG_ENTERED if the drag shadow
is still within the View object's bounding box.
ACTION_DRAG_EXITED
4 Signals that the user has moved the drag shadow outside the bounding
box of the View.
ACTION_DROP
5 Signals to a View that the user has released the drag shadow, and the
drag point is within the bounding box of the View.
ACTION_DRAG_ENDED
6
Signals to a View that the drag and drop operation has concluded.
Methods
Following are few important and most frequently used methods available as a part of DragEvent class.
Sr.No. Constants & Description
int getAction()
1
Inspect the action value of this event.
ClipData getClipData()
2 Returns the ClipData object sent to the system as part of the call to
startDrag().
ClipDescription getClipDescription()
3
Returns the ClipDescription object contained in the ClipData.
boolean getResult()
4
Returns an indication of the result of the drag and drop operation.
float getX()
5
Gets the X coordinate of the drag point.
float getY()
6
Gets the Y coordinate of the drag point.
String toString()
7
Returns a string representation of this DragEvent object.
2
BCA 502, Mobile Application Development Batch 2021-2024
Dr Chandani Sharma Unit 3
M.M. Institute of Computer Technology & Business Management
Example
Following example shows the functionality of a simple Drag & Drop
using View.setOnLongClickListener(), View.setOnTouchListener()and View.OnDragEventListene
r().
Step Description
You will use Android studio IDE to create an Android application and
1 name it as My Application under a
package com.example.saira_000.myapplication.
Modify src/MainActivity.java file and add the code to define event
2 listeners as well as a call back methods for the logo image used in the
example.
Copy image abc.png in res/drawable-* folders. You can use images with
3 different resolution in case you want to provide them for different
devices.
Modify layout XML file res/layout/activity_main.xml to define default
4
view of the logo images.
Run the application to launch Android emulator and verify the result of
5
the changes done in the application.
Following is the content of the modified main activity file src/MainActivity.java. This file can include
each of the fundamental lifecycle methods.
package com.example.saira_000.myapplication;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipDescription;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
3
BCA 502, Mobile Application Development Batch 2021-2024
Dr Chandani Sharma Unit 3
M.M. Institute of Computer Technology & Business Management
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView)findViewById(R.id.imageView);
img.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
v.startDrag(dragData,myShadow,null,0);
return true;
}
});
img.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
switch(event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
layoutParams = (RelativeLayout.LayoutParams)v.getLayoutParams();
Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED");
// Do nothing
break;
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();
break;
case DragEvent.ACTION_DRAG_EXITED :
4
BCA 502, Mobile Application Development Batch 2021-2024
Dr Chandani Sharma Unit 3
M.M. Institute of Computer Technology & Business Management
case DragEvent.ACTION_DRAG_LOCATION :
Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_ENDED :
Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENDED");
// Do nothing
break;
case DragEvent.ACTION_DROP:
Log.d(msg, "ACTION_DROP event");
// Do nothing
break;
default: break;
}
return true;
}
});
img.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(img);
5
BCA 502, Mobile Application Development Batch 2021-2024
Dr Chandani Sharma Unit 3
M.M. Institute of Computer Technology & Business Management
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Drag and Drop Example"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials Point"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:textSize="30dp"
android:textColor="#ff14be3c" />>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc"
android:layout_below="@+id/textView2"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2" />
</RelativeLayout>
Following will be the content of res/values/strings.xml to define two new constants −
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My Application</string>
</resources>
6
BCA 502, Mobile Application Development Batch 2021-2024
Dr Chandani Sharma Unit 3
M.M. Institute of Computer Technology & Business Management
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>