Drag and Drop (Honeycomb/ICS)

      12 Comments on Drag and Drop (Honeycomb/ICS)
Drag and drop is already available with Honeycomb and up. In this example, we will see how to implement a simple Drag and Drop Operation.

We have a ListView with some items. The other half of the screen would have a drop area which has a single TextView. We will see, how to drag items from the ListView, drop them into the drop area, which will update the TextView with the list item’s title, and also, that particular item would be removed from the ListView.

We will trigger the drag operation, when you long tap on an item in the ListView. After the drag operation is started, you will see a floating view of the selected item, which you can move around the screen. When you approach the drop area, the background of the drop area would change, which means that you can now drop your item. Once you leave the item, the TextView inside the drop area will show the text of the selected item.

This should also work on Ice Cream Sandwich. You can find the whole source code here. The code is quite self-explanatory. But, if you have any queries, let me know through the comment form. There are a few comments in the code to help you.

12 thoughts on “Drag and Drop (Honeycomb/ICS)

  1. JSbuTa

    Hi I managed to get it working thanks for the code. I'm wondering if it is possible to drag the items in the listview into the drop area and then allow the items to be dragged around inside the drop area? Thanks! Would hope to hear a reply from you!

  2. K

    Hi. I'm actually doing a project to regard of drag and drop. I came across your post and found it really useful. However, I encounter quite a few errors:

    import android.content.ClipData;
    import android.content.ClipData.Item;
    import android.content.ClipDescription;
    import android.view.DragEvent;
    import android.view.View.OnDragListener;

    Please help as i'm a new learner to Java šŸ™

  3. Anonymous

    hi i want to make the animation base game so i want drop the target any time so please say me how to drop the target in runtime please help me yar
    thanks in advance

Leave a Reply