I am working in android, I am using a button. Now I want to perform drag and drop of this button.
This is my main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_first"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="drag me"
/>
</LinearLayout>
And this is my code to move this button on mouse drag:-
public class DragdropActivity extends Activity implements OnTouchListener {
private final static int START_DRAGGING = 0;
private final static int STOP_DRAGGING = 1;
private Button btn;
private int status;
private ImageView image;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn);
btn.setDrawingCacheEnabled(true);
btn.setOnTouchListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
status = START_DRAGGING;
}
if (me.getAction() == MotionEvent.ACTION_UP) {
status = STOP_DRAGGING;
Log.i("Drag", "Stopped Dragging");
} else if (me.getAction() == MotionEvent.ACTION_MOVE) {
if (status == START_DRAGGING) {
System.out.println("Dragging");
Log.v("***Drag and drop **",
"me.getRawX and Y = " + me.getRawX() + " "
+ me.getRawY());
Log.v("***Drag and drop **",
"image position = " + image.getLeft() + " "
+ image.getRight());
btn.setPadding((int) me.getRawX(), (int) me.getRawY(), 0,0); //this is not working fine.
btn.invalidate();
}
}
return false;
}
}
I think btn.setPadding()
is not working correctly, please suggest me what should I do so that the button can easily move to the position of mouse or gesture touch ?
A drag and drop operation starts when the user makes a UI gesture that your app recognizes as a signal to start dragging data. In response, the app notifies the system that a drag and drop operation is starting. The system calls back to your app to get a representation of the data being dragged (a drag shadow).
What is Action_drag_ended meant for? Ended − Just after the action type ACTION_DROP, the system sends out a drag event with action type ACTION_DRAG_ENDED to indicate that the drag operation is over.
The HTML 5 drag'n'drop API allows you to implement drag'n'drop on most desktop browsers and some mobile browsers. Unfortunately, you'll notice most mobile browsers don't support it, so no iPad (or Nexus) action for you!
I resolved my problem like this.
@Override
public boolean onTouch(View view, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
status = START_DRAGGING;
}
if (me.getAction() == MotionEvent.ACTION_UP) {
status = STOP_DRAGGING;
Log.i("Drag", "Stopped Dragging");
} else if (me.getAction() == MotionEvent.ACTION_MOVE) {
if (status == START_DRAGGING) {
System.out.println("Dragging");
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
50, 50);
layoutParams.setMargins((int) me.getRawX() - 25,
(int) me.getRawY() - 50, 0, 0);
layout.removeView(btn);
layout.addView(btn, layoutParams);
btn.invalidate();
}
}
return false;
}
Now its working fine.
you should get button's layout params, set left, right, top, bottom and set these layout params back to button
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With