The following code helps you to do a drag and drop in ANDROID.
package com.pack; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.os.Bundle; import android.view.MotionEvent; import android.widget.AbsoluteLayout; import android.widget.Button; import android.widget.ImageView; public class Touch extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MyView tx = new MyView(this); MyView2 tx2 = new MyView2(this); tx.setText("Drag Me"); int imgID = getResources().getIdentifier("icon", "drawable", "com.pack"); tx2.setImageResource(imgID); AbsoluteLayout l = new AbsoluteLayout(this); AbsoluteLayout.LayoutParams p = new AbsoluteLayout.LayoutParams( AbsoluteLayout.LayoutParams.WRAP_CONTENT, AbsoluteLayout.LayoutParams.WRAP_CONTENT,0,0); l.addView(tx,p); l.addView(tx2,p); setContentView(l); } // events when touching the screen public boolean onTouchEvent(MotionEvent event) { int eventaction = event.getAction(); int X = (int)event.getX(); int Y = (int)event.getY(); System.out.println("SCR X="+X+" SCR y="+Y); switch (eventaction ) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: break; } return true; } /******************* Class 1 ************************************/ class MyView extends Button { public MyView(Context c){ super(c); } @Override public boolean dispatchTouchEvent(MotionEvent event) { int mCurX = (int)event.getRawX(); int mCurY = (int)event.getRawY();; System.out.println("scrollx "+this.getScrollX()); int action = event.getAction(); if ( action == MotionEvent.ACTION_MOVE ) { System.out.println("X="+mCurX+" y="+mCurY); this.setText("x: " + mCurX + ",y: " + mCurY ); AbsoluteLayout.LayoutParams p = new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT, AbsoluteLayout.LayoutParams.WRAP_CONTENT,mCurX-50,mCurY-50); this.setLayoutParams (p); } return true; } public void draw(Canvas canvas) { super.draw(canvas); } } /******************* Class 2 ************************************/ class MyView2 extends ImageView { public MyView2(Context c){ super(c); } @Override public boolean dispatchTouchEvent(MotionEvent event) { int mCurX = (int)event.getRawX(); int mCurY = (int)event.getRawY();; System.out.println("scrollx "+this.getScrollX()); int action = event.getAction(); if ( action == MotionEvent.ACTION_MOVE ) { System.out.println("X="+mCurX+" y="+mCurY); int imgID = getResources().getIdentifier("icon", "drawable", "com.pack"); this.setImageResource(imgID); AbsoluteLayout.LayoutParams p = new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT, AbsoluteLayout.LayoutParams.WRAP_CONTENT,this.getScrollX()+ mCurX,this.getScrollY()+mCurY); this.setLayoutParams (p); } return true; } @Override public void draw(Canvas canvas) { super.draw(canvas); } } }
i want the same one in grid view with drag and drop funtionality can u plz do dat