Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
MainActivity.java

Java
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipDescription;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

	ImageView ima;
	private static final String IMAGEVIEW_TAG = "Android Logo";
	String msg;

	private android.widget.RelativeLayout.LayoutParams layoutParams;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ima=(ImageView)findViewById(R.id.iv_logo);
		
		//Sets the tag
		
		ima.setTag(IMAGEVIEW_TAG);
		
		ima.setOnLongClickListener(new View.OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				
				ClipData.Item item=new ClipData.Item((CharSequence) v.getTag());
				
				String[] mymeTypes={ClipDescription.MIMETYPE_TEXT_PLAIN};
				ClipData dragData=new ClipData(v.getTag().toString(), mymeTypes, item);
				
				//Instatiate the drag Shadow Builder
				
				View.DragShadowBuilder myShadow=new DragShadowBuilder(ima);
				
				//Starts the Drag
				
				v.startDrag(dragData, //The data to be dragged 
						myShadow, //the dragShoadow Builder
						null,
						0 //Flags not currently used set to 0
						);
				return true;
			}
		});
		
		//Create and set the drag event listener for the View
		
		ima.setOnDragListener(new OnDragListener(){
			@Override
			public boolean onDrag(View v, DragEvent event) {
				// TODO Auto-generated method stub
				
				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
					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:
					Log.d(msg,"Action is DragEvent.ACTION_DRAG_EXITED");
					x_cord=(int)event.getX();
					y_cord=(int)event.getY();
					break;
					
				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");
					break;
					
				case DragEvent.ACTION_DROP:
					Log.d(msg,"Action is DragEvent.ACTION_DROP");
					break;
					
					default:
						break;
						
				
				}
				return true;
				
			}
		});
	}

}

and activity.xml is as follows
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/drag_drop"
        android:src="@drawable/ic_launcher">

</relativelayout>
Posted
Updated 17-Aug-14 22:05pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900