Drag an ImageView to a particular place on a screen in android
<p>I am implementing Drag and Drop functionality with ImageView in android, but I received an error every time when I want to touch the view. I don't know how this problem could be solved? Please suggest me for the right solution regarding the same.</p>

<p><strong>Error-Stack:</strong></p>

<pre><code>01-16 18:32:31.349: E/AndroidRuntime(3157): FATAL EXCEPTION: main
01-16 18:32:31.349: E/AndroidRuntime(3157): java.lang.IllegalArgumentException: Given view not a child of com.SpeachTreat.common.DragLayer@44c35318
01-16 18:32:31.349: E/AndroidRuntime(3157):     at android.view.ViewGroup.updateViewLayout(ViewGroup.java:1876)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at com.SpeachTreat.common.DragLayer.onDrop(DragLayer.java:90)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at com.SpeachTreat.common.DragController.drop(DragController.java:446)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at com.SpeachTreat.common.DragController.onTouchEvent(DragController.java:424)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at com.SpeachTreat.common.DragLayer.onTouchEvent(DragLayer.java:63)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at android.view.View.dispatchTouchEvent(View.java:3766)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1676)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1112)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1660)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at android.os.Looper.loop(Looper.java:123)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at android.app.ActivityThread.main(ActivityThread.java:4627)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at java.lang.reflect.Method.invokeNative(Native Method)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at java.lang.reflect.Method.invoke(Method.java:521)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-16 18:32:31.349: E/AndroidRuntime(3157):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-16 18:32:31.349: E/AndroidRuntime(3157):     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>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);
    }

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 (!SpeachTreatActivity.Debugging) return;
    Toast.makeText (getContext (), msg, Toast.LENGTH_SHORT).show ();
} // end toast
</code></pre>

<p><strong>My Activity:</strong></p>

<pre><code>public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        img_character = (ImageView)findViewById(R.id.img_Treat_Character);
        img_treat1 = (ImageView)findViewById(R.id.img_treat1);
        img_treat2 = (ImageView)findViewById(R.id.img_treat2);
        img_treat3 = (ImageView)findViewById(R.id.img_treat3);
        img_treat4 = (ImageView)findViewById(R.id.img_treat4);
        img_treat5 = (ImageView)findViewById(R.id.img_treat5);
        img_treat6 = (ImageView)findViewById(R.id.img_treat6);
        img_treat7 = (ImageView)findViewById(R.id.img_treat7);
        img_treat8 = (ImageView)findViewById(R.id.img_treat8);
        img_treat9 = (ImageView)findViewById(R.id.img_treat9);
        img_treat10 = (ImageView)findViewById(R.id.img_treat10);

        img_treat1.setOnLongClickListener(this);
        img_treat2.setOnLongClickListener(this);
        img_treat3.setOnLongClickListener(this);
        img_treat4.setOnLongClickListener(this);
        img_treat5.setOnLongClickListener(this);
        img_treat6.setOnLongClickListener(this);
        img_treat7.setOnLongClickListener(this);
        img_treat8.setOnLongClickListener(this);
        img_treat9.setOnLongClickListener(this);
        img_treat10.setOnLongClickListener(this);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
    @Override
    public boolean onLongClick(View v) {
        // TODO Auto-generated method stub
         trace ("onLongClick in view: " + v);

            // Make sure the drag was started by a long press as opposed to a long click.
            // (Note: I got this from the Workspace object in the Android Launcher code. 
            //  I think it is here to ensure that the device is still in touch mode as we start the drag operation.)
            if (!v.isInTouchMode()) {
                Toast.makeText(this, "isInTouchMode returned false. Try touching the view again.", Toast.LENGTH_SHORT).show();
               return false;
            }
            return startDrag (v);
    }
    public void trace (String msg) 
    {
        if (!Debugging) return;
        Log.d ("SpeachTreatActivity", msg);
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
    public boolean startDrag (View v)
    {
        // Let the DragController initiate a drag-drop sequence.
        // I use the dragInfo to pass along the object being dragged.
        // I'm not sure how the Launcher designers do this.
        Object dragInfo = v;
        mDragController.startDrag (v, mDragLayer, dragInfo, DragController.DRAG_ACTION_MOVE);
        return true;
    }
</code></pre>
