title: Dropping an image to a particular Place in Android
body: <p>I have a problem that I am using Drag n Drop functionality in android 2.2 version, I received an error every time when we drop an image to another place.......Dragging works fine but Drop returns an IllegalArgumentException Error.</p>
  
  <p><strong>Error Stack:</strong></p>
  
  <pre><code>01-16 20:27:46.029: E/AndroidRuntime(3931): FATAL EXCEPTION: main
  01-16 20:27:46.029: E/AndroidRuntime(3931): java.lang.IllegalArgumentException: Given view not a child of com.SpeachTreat.common.DragLayer@44c95318
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.view.ViewGroup.updateViewLayout(ViewGroup.java:1876)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at com.SpeachTreat.common.DragLayer.onDrop(DragLayer.java:90)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at com.SpeachTreat.common.DragController.drop(DragController.java:446)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at com.SpeachTreat.common.DragController.onTouchEvent(DragController.java:424)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at com.SpeachTreat.common.DragLayer.onTouchEvent(DragLayer.java:63)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.view.View.dispatchTouchEvent(View.java:3766)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1676)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1112)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1660)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.os.Handler.dispatchMessage(Handler.java:99)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.os.Looper.loop(Looper.java:123)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at android.app.ActivityThread.main(ActivityThread.java:4627)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at java.lang.reflect.Method.invokeNative(Native Method)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at java.lang.reflect.Method.invoke(Method.java:521)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
  01-16 20:27:46.029: E/AndroidRuntime(3931):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p><strong>Xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;com.SpeachTreat.common.DragLayer
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"
  android:background="#ffffff"
      android:id="@+id/drag_layer"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"&gt;
  
  
  &lt;LinearLayout 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="#ffffff"
      android:layout_centerVertical="true"
      android:orientation="vertical" &gt;
  
      &lt;RelativeLayout
          android:id="@+id/relativeLayout1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" &gt;
  
          &lt;LinearLayout
              android:id="@+id/linearLayout1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentTop="true"
              android:layout_centerHorizontal="true" android:background="@drawable/header_back" android:orientation="vertical"&gt;
  
              &lt;ImageView
                  android:id="@+id/imageView1"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:src="@drawable/logo" /&gt;
  
              &lt;ImageView
                  android:id="@+id/imageView2"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:src="@drawable/header_text" /&gt;
  
          &lt;/LinearLayout&gt;
  
      &lt;/RelativeLayout&gt;
  
      &lt;RelativeLayout
          android:id="@+id/relativeLayout2"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" android:layout_marginTop="10dip"&gt;
  
          &lt;FrameLayout
              android:id="@+id/frameLayout1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentTop="true"
              android:layout_centerHorizontal="true" android:background="@drawable/start_playing_with_outline4"&gt;
  
              &lt;ImageView
                  android:id="@+id/img_Treat_Character"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:src="@drawable/cute_animal_big3" android:layout_gravity="center_vertical|center_horizontal"/&gt;
  
          &lt;/FrameLayout&gt;
  
      &lt;/RelativeLayout&gt;
  
      &lt;RelativeLayout
          android:id="@+id/relativeLayout3"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" android:layout_marginTop="10dip"&gt;
  
          &lt;LinearLayout
              android:id="@+id/linearLayout2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentTop="true"
              android:layout_centerHorizontal="true"
              android:orientation="vertical" android:background="@drawable/select_your_treat_with_outline"&gt;
  
               &lt;LinearLayout
                  android:id="@+id/linearLayout4"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" android:layout_marginTop="10dip"&gt;
  
                  &lt;ImageView
                      android:id="@+id/img_treat1"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/treat1" android:layout_marginLeft="30dip"/&gt;
                  &lt;ImageView
                      android:id="@+id/img_treat2"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/treat1" android:layout_marginLeft="10dip"/&gt;
                  &lt;ImageView
                      android:id="@+id/img_treat3"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/treat1" android:layout_marginLeft="10dip"/&gt;
                  &lt;ImageView
                      android:id="@+id/img_treat4"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/treat1" android:layout_marginLeft="10dip"/&gt;
                  &lt;ImageView
                      android:id="@+id/img_treat5"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/treat1" android:layout_marginLeft="10dip"/&gt;
  
              &lt;/LinearLayout&gt;
               &lt;LinearLayout
                  android:id="@+id/linearLayout5"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" android:layout_marginTop="10dip"&gt;
  
                  &lt;ImageView
                      android:id="@+id/img_treat6"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/treat1" android:layout_marginLeft="30dip"/&gt;
                  &lt;ImageView
                      android:id="@+id/img_treat7"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/treat1" android:layout_marginLeft="10dip"/&gt;
                  &lt;ImageView
                      android:id="@+id/img_treat8"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/treat1" android:layout_marginLeft="10dip"/&gt;
                  &lt;ImageView
                      android:id="@+id/img_treat9"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/treat1" android:layout_marginLeft="10dip"/&gt;
                  &lt;ImageView
                      android:id="@+id/img_treat10"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/treat1" android:layout_marginLeft="10dip"/&gt;
  
              &lt;/LinearLayout&gt;
  
          &lt;/LinearLayout&gt;
  
      &lt;/RelativeLayout&gt;
   &lt;RelativeLayout
          android:id="@+id/relativeLayout4"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" android:layout_marginTop="10dip"&gt;
  
      &lt;LinearLayout
          android:id="@+id/linearLayout3"
          android:layout_width="wrap_content"
           android:layout_alignParentTop="true"
              android:layout_centerHorizontal="true"
          android:layout_height="wrap_content" android:layout_marginTop="10dip"&gt;
  
          &lt;ImageView
              android:id="@+id/imageView3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:src="@drawable/selectanewcharacter_button" /&gt;
  
          &lt;ImageView
              android:id="@+id/imageView4"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:src="@drawable/end_game_button" android:layout_marginTop="10dip"/&gt;
  
  
          &lt;ImageView
              android:id="@+id/imageView5"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:src="@drawable/green_button_with_shadow" android:layout_marginTop="20dip" android:layout_marginLeft="10dip"/&gt;
  
          &lt;ImageView
              android:id="@+id/imageView6"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:src="@drawable/red_button_with_shadow" android:layout_marginTop="20dip" android:layout_marginLeft="10dip"/&gt;
  
      &lt;/LinearLayout&gt;
  &lt;/RelativeLayout&gt;
  &lt;/LinearLayout&gt;
  &lt;/com.SpeachTreat.common.DragLayer&gt;
  </code></pre>
  
  <p><strong>DragLayer:</strong></p>
  
  <pre><code>package com.blahti.example.drag;
  
  import android.content.Context;
  import android.graphics.Rect;
  import android.util.AttributeSet;
  import android.view.MotionEvent;
  import android.view.KeyEvent;
  import android.view.View;
  import android.widget.Toast;
  
  public class DragLayer extends MyAbsoluteLayout 
      implements DragSource, DropTarget
  {
      DragController mDragController;
  
      /**
       * Used to create a new DragLayer from XML.
       *
       * @param context The application's context.
       * @param attrs The attribtues set containing the Workspace's customization values.
       */
      public DragLayer (Context context, AttributeSet attrs) {
          super(context, attrs);
      }
  
      public void setDragController(DragController controller) {
          mDragController = controller;
      }
  
      @Override
      public boolean dispatchKeyEvent(KeyEvent event) {
          return mDragController.dispatchKeyEvent(event) || super.dispatchKeyEvent(event);
      }
  
      @Override
      public boolean onInterceptTouchEvent(MotionEvent ev) {
          return mDragController.onInterceptTouchEvent(ev);
      }
  
      @Override
      public boolean onTouchEvent(MotionEvent ev) {
          return mDragController.onTouchEvent(ev);
      }
  
      @Override
      public boolean dispatchUnhandledMove(View focused, int direction) {
          return mDragController.dispatchUnhandledMove(focused, direction);
      }
  
  /**
   */
  // DragSource interface methods
  
  /**
   * setDragController
   *
   */
  
   /* setDragController is already defined. See above. */
  
  /**
   * onDropCompleted
   *
   */
  
  public void onDropCompleted (View target, boolean success)
  {
      toast ("DragLayer2.onDropCompleted: " + target.getId () + " Check that the view moved.");
  }
  
  public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
          DragView dragView, Object dragInfo)
  {
      View v = (View) dragInfo;
      toast ("DragLayer2.onDrop accepts view: " + v.getId ()
            + "x, y, xO, yO :" + new Integer (x) + ", " + new Integer (y) + ", "
            + new Integer (xOffset) + ", " + new Integer (yOffset));
  
      int w = v.getWidth ();
      int h = v.getHeight ();
      int left = x - xOffset;
      int top = y - yOffset;
      DragLayer.LayoutParams lp = new DragLayer.LayoutParams (w, h, left, top);
      this.updateViewLayout(v, lp);
  }
  
  public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
          DragView dragView, Object dragInfo)
  {
  }
  
  public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
          DragView dragView, Object dragInfo)
  {
  }
  
  public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
          DragView dragView, Object dragInfo)
  {
  }
  
  
  public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
          DragView dragView, Object dragInfo)
  {
      return true;
  }
  
  
  public Rect estimateDropLocation(DragSource source, int x, int y, int xOffset, int yOffset,
              DragView dragView, Object dragInfo, Rect recycle)
  {
      return null;
  }
  
  public void toast (String msg)
  {
      if (!DragActivity.Debugging) return;
      Toast.makeText (getContext (), msg, Toast.LENGTH_SHORT).show ();
  } // end toast
  
  
  } // end class
  </code></pre>