Simple Drag n Drop on Android

      52 Comments on Simple Drag n Drop on Android

Till now, there is no Drag and Drop like control on the Android. This feature might come in handy in a few situations to improve the usability of your apps. Here is a simple Drag and Drop app which allows you to drag a button and drop it anywhere on the screen.
Theory: Go for a FrameLayout.
FrameLayout is designed to block out an area on the screen to display a single item. You can add multiple children to a FrameLayout, but all children are pegged to the top left of the screen. Children are drawn in a stack, with the most recently added child on top.
In our main layout, we have a single button. We write a touch listener which will track the touch events and also help us to move the button around the screen. In this example, we will not be moving the button, but it’s image which we set to a ImageView. Also, instead of actually moving the ImageView, we will change the padding of the ImageView as the mouse moves, which will give an impression of the ImageView being moved.
The source code for this can be found on this link.
Drag and Drop example

52 thoughts on “Simple Drag n Drop on Android

  1. nihar

    If your replace button with TextView then the text will get stagnant at one place and over rights. can you please tell me how to do drag n drop with TextView

  2. Dave Haveman

    This looks pretty cool, anyway you could also add a touch listener to each object added that removes it? I tried this and it just removes the latest imageview after each touch

  3. Anonymous

    this looks great, can you modify the code so that only one button is there and the same button is moved from different places to some other place.

  4. Sushant Bhatnagar

    Thanks For Help . I want to drag button/textview or any control to inside linear layout/relative layout . Then if i move control in layout , control cannot move outside from layout . It move only inside the layout.
    If I drop control outside layout that , it go back to it original position.

  5. Anonymous

    Nice Example but how to use the image instead of button?
    how to drop the button means redrop and reDrag?
    Thank in advance

  6. Anonymous

    if are you not interested in dive the reply of any question?
    then why create this type blog?
    ok so please give any sutable answer of question if yuo know!
    else say no that u don`t know about this ok

    thank u

    1. Kumar Bibek Post author

      This is not a forum. And, morever, I don't sit all day long to reply to comments I get. This blog is an attempt to put forth some samples that would try to solve some problems. This is not a place where you would get fully working code that you would be able to directly copy and paste into your project. You will also have to do your own research. I am generally happy to reply to comments, whenever I get time, and if a question makes sense.

      If you ask questions, which are not very clear, it's neither going to help you or me.

      Also, I tend to ignore comments from "Anonymous" users. Sorry for the inconvenience anyway.

  7. Anonymous

    someone know why copying the code i get a "method onTouch must override a superclass method" as I said i copied the java code, thanks in advance

    1. Anonymous

      my class is as follows: public void name extends activity implements OnTouchListener{}

      I've also extended application itself but I don't think that's the problem, thanks

  8. Anonymous

    hello Kumar Bibek,

    I just have one query, just want to ask if, after onMove method, the Button view still exits or is it just another image created

  9. Erik

    Hab das gleiche mal mit ner ImageView und einen ImageButton probiert. Aber es funktioniert nicht. Sieht quasi so aus.
    public class Dragndrop2Activity extends Activity implements OnTouchListener {

    private final static int START_DRAGGING = 0;
    private final static int STOP_DRAGGING = 1;

    private ImageButton ibtn;
    private ImageView iv1;
    private FrameLayout layout;
    private int status;
    private LayoutParams params;

    private ImageView image;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

    layout = (FrameLayout) findViewById(;
    // layout.setOnTouchListener(this);

    ibtn = (ImageButton) findViewById(;
    iv1 = (ImageView) findViewById(;

    params = new LayoutParams(LayoutParams.WRAP_CONTENT,


    public boolean onTouch(View view, MotionEvent me) {
    if (me.getAction() == MotionEvent.ACTION_DOWN) {
    status = START_DRAGGING;
    image = new ImageView(this);
    layout.addView(image, params);
    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) {
    image.setPadding((int) me.getRawX(), (int) me.getRawY(), 0, 0);
    return false;

    Wo ist der Fehler?

  10. Pete


    unfortunatelly, I cannot open Source code page.

    Could you please send me correct link, or code into email?

    Thank you in advance.


Leave a Reply

Your email address will not be published. Required fields are marked *