title: How to drag a imageview in android studio?
body: <p>I'm using this tutorial to create a imageview and drag it over the screen</p>
  
  <p><a href="http://www.tutorialspoint.com/android/android_drag_and_drop.htm" rel="nofollow">http://www.tutorialspoint.com/android/android_drag_and_drop.htm</a></p>
  
  <p>but when i drag the image in my emulator and i drop it disappears! how can i fix that?</p>
  
  <p>here is my layout file</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin"
      tools:context=".MainActivity"&gt;
  
      &lt;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" /&gt;
  
      &lt;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" /&gt;&gt;
  
      &lt;ImageView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/imageView"
          android:src="@mipmap/ic_launcher"
          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" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>and here is my java activity file</p>
  
  <pre><code>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;
  import android.view.View;
  import android.widget.ImageView;
  import android.widget.RelativeLayout;
  
  
  public class MainActivity extends Activity {
      ImageView img;
      String msg;
      private android.widget.RelativeLayout.LayoutParams layoutParams;
  
      @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};
  
                  ClipData dragData = new ClipData(v.getTag().toString(),mimeTypes, item);
                  View.DragShadowBuilder myShadow = new View.DragShadowBuilder(img);
  
                  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 :
                          Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED");
                          x_cord = (int) event.getX();
                          y_cord = (int) event.getY();
                          layoutParams.leftMargin = x_cord;
                          layoutParams.topMargin = y_cord;
                          v.setLayoutParams(layoutParams);
                          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");
  
                          // 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);
  
                      img.startDrag(data, shadowBuilder, img, 0);
                      img.setVisibility(View.INVISIBLE);
                      return true;
                  }
                  else
                  {
                      return false;
                  }
              }
          });
      }
  
  }
  </code></pre>
  
  <p>Thanks in advance!</p>