title: dragging image to another relative layout android
body: <p>I am creating a drag and drop android application, where I need to drag the objects(star, heart) on the relative layout. </p>
  
  <p>My problem is everytime I drag the object it goes anywhere.</p>
  
  <p>this is my code:</p>
  
  <pre><code>public class MainActivity extends Activity {    
  int _xDelta;
  int _yDelta;
    @Override
    public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());
  findViewById(R.id.myimage2).setOnTouchListener(new MyTouchListener());
  findViewById(R.id.myimage3).setOnTouchListener(new MyTouchListener());
  
  findViewById(R.id.left).setOnDragListener(new MyDragListener());
  findViewById(R.id.right).setOnDragListener(new MyDragListener());
  
    }
  </code></pre>
  
  <p>This is my touch listener:</p>
  
  <pre><code>private final class MyTouchListener implements OnTouchListener {
  public boolean onTouch(View view, MotionEvent motionEvent) {
  
         final int X = (int) motionEvent.getRawX();
          final int Y = (int) motionEvent.getRawY();
          switch (motionEvent.getAction() &amp; MotionEvent.ACTION_MASK) {
              case MotionEvent.ACTION_DOWN:
                  LinearLayout.LayoutParams lParams = (LinearLayout.LayoutParams) view.getLayoutParams();
                  _xDelta = X - lParams.leftMargin;
                  _yDelta = Y - lParams.topMargin;
  
                  ClipData data = ClipData.newPlainText("", "");
                  DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
                  view.startDrag(data, shadowBuilder, view, 0);
                  view.setVisibility(View.INVISIBLE);
  
                  break;
              case MotionEvent.ACTION_UP:
                  break;
              case MotionEvent.ACTION_POINTER_DOWN:
                  break;
              case MotionEvent.ACTION_POINTER_UP:
                  break;
              case MotionEvent.ACTION_MOVE:
                  LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
                  layoutParams.leftMargin = X - _xDelta;
                  layoutParams.topMargin = Y - _yDelta;
                  layoutParams.rightMargin = -250;
                  layoutParams.bottomMargin = -250;
                  view.setLayoutParams(layoutParams);
  
                  break;
          }
          return true;
  }
    }
  </code></pre>
  
  <p>And this is my drag listener:</p>
  
  <pre><code>class MyDragListener implements OnDragListener {
  @Override
  public boolean onDrag(View v, DragEvent event) {
    int action = event.getAction();
    switch (event.getAction()) {
    case DragEvent.ACTION_DRAG_STARTED:
      break;
    case DragEvent.ACTION_DRAG_ENTERED:
      break;
    case DragEvent.ACTION_DRAG_EXITED:
      break;
    case DragEvent.ACTION_DROP:
      // Dropped, reassign View to ViewGroup
      View view = (View) event.getLocalState();
      ViewGroup owner = (ViewGroup) view.getParent();
      owner.removeView(view);
      LinearLayout container = (LinearLayout) v;
      container.addView(view);
      view.setVisibility(View.VISIBLE);
      break;
    case DragEvent.ACTION_DRAG_ENDED:
      default:
      break;
    }
    return true;
  }
    }
  </code></pre>
  
  <p>This is my layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:columnCount="2"
  android:columnWidth="300dp"
  android:orientation="vertical"
  android:rowCount="2"
  android:stretchMode="columnWidth" &gt;
  
  &lt;LinearLayout
      android:id="@+id/topleft"
      android:layout_width="478dp"
      android:layout_height="200dp"
      android:layout_column="0"
      android:layout_row="0"
      android:background="@drawable/shape" 
      android:orientation="vertical"
      &gt;
  
  &lt;/LinearLayout&gt;
  
  &lt;LinearLayout
      android:id="@+id/topright"
      android:layout_width="wrap_content"
      android:layout_height="200dp"
      android:layout_column="1"
      android:layout_gravity="left"
      android:layout_row="0"
      android:background="@drawable/shape"
      android:orientation="vertical" &gt;
  
      &lt;ImageView
          android:id="@+id/myimage2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_column="0"
          android:layout_row="0"
          android:src="@drawable/ic_launcher" /&gt;
  
      &lt;ImageView
          android:id="@+id/myimage1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_column="0"
          android:layout_row="0"
          android:src="@drawable/ic_launcher" /&gt;
  
      &lt;ImageView
          android:id="@+id/myimage3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_launcher" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p> </p>
  
  <p>Hope you can help me! Thanks</p>