1

How to design movable (Drag/Drop) view in android.

In my case enter image description here

There will be four rows and all will be movable.

User can arrange all 4 rows like 1234, 2341, 3421, 1243...... any combination of 1234.

So how can it will be?

All rows are layouts.

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186

1 Answers1

1

Have a members declared for holding the currently dragging bitmap, view, and current dragging x and y coords.

  • Override the onTouch of the parent view.
  • In ACTION_DOWN event get the drawing cache(Bitmap) of the dragging view by calling getDrawingCache() and set to the drag bitmap member. Invalidate the layout (so that onDraw gets called, where you draw the currently dragging view). Hide the current dragging view.
  • In ACTION_MOVE draw (invalidate) the dragging view bitmap at current x and y coordinates.
  • In ACTION_UP destroy the dragging bitmap. calculate the current drop location and add the current dragging view to that position in the layout. UnHide the current dragging view. Reset the dragging members.

In onDraw of parent layout.

canvas.save();
canvas.drawBitmap(mDragBmp, mDragX, mDragY);
canvas.restore();
halfer
  • 19,824
  • 17
  • 99
  • 186
Ron
  • 24,175
  • 8
  • 56
  • 97
  • it will be more helpful if you add some links please. – Pankaj Kumar Aug 19 '11 at 04:46
  • its not as difficult as it looks. I dint find any links to a solution as simple as this. U can try using the android's DragLayer implementation (from android code) which will take atleast a day. – Ron Aug 19 '11 at 05:07
  • Thanks, At this time I am not implementing your logic but it was very useful for me, and I will use in future. – Pankaj Kumar Aug 30 '11 at 08:25