I want to make a drag and drop button. Drag it where you want it to do and it stays there. Below code only scales the button, doesn\'t change its position.
p
Maybe the answers should be improved, because this question is still very prominent when you google fot it.
Today there is a Framework which can be used for drag and drop. It was introduced with Android 4.
You can find a nice tutorial at Google developer page:
https://developer.android.com/guide/topics/ui/drag-drop.html
try out my solution, which doesn't re-create layutParams and also handles the edges nicely:
https://stackoverflow.com/a/18806475/878126
I am working on something similar to this. Here is the OnTouchListener that I am using:
myOnTouchListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent me){
if (me.getAction() == MotionEvent.ACTION_DOWN){
oldXvalue = me.getX();
oldYvalue = me.getY();
Log.i(myTag, "Action Down " + oldXvalue + "," + oldYvalue);
}else if (me.getAction() == MotionEvent.ACTION_MOVE ){
LayoutParams params = new LayoutParams(v.getWidth(), v.getHeight(),(int)(me.getRawX() - (v.getWidth() / 2)), (int)(me.getRawY() - (v.getHeight())));
v.setLayoutParams(params);
}
return true;
}
};
v is the view that you are wanting to move, in your case it you'd replace v with your button. Also note that in order to get this to work I had to use an AbsoluteLayout as the parent view in my xml file. I know that it is deprecated but it seemed more logical to use that then a RelativeLayout and trying to set the margins dynamically to move the view around. The formula that I used for the new x and y positions tries to make it so that the view is centered on your finger while you are moving it. But its not quite perfect, depending on the size of the view it will still be a little off center from your finger.
I know this question is a little old, but I found it when trying to implement drag and drop for a button. Using FoamyGuy's method I was able to get drag and drop to work using a LinearLayout as the parent view by setting the margins of the view in the onTouch method.
dragBtn.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_MOVE ){
LayoutParams params = new LayoutParams(v.getWidth(), v.getHeight());
//set the margins. Not sure why but multiplying the height by 1.5 seems to keep my finger centered on the button while it's moving
params.setMargins((int)me.getRawX() - v.getWidth()/2, (int)(me.getRawY() - v.getHeight()*1.5), (int)me.getRawX() - v.getWidth()/2, (int)(me.getRawY() - v.getHeight()*1.5));
v.setLayoutParams(params);
}
return true;
}
});
NOTE: This only works if the view that you are dragging is the last view in the hierarchy. Otherwise it moves all the other views in relation to the view that you are dragging.