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. 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.

  2. 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.

  3. 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. 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.

  4. 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. 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

  5. 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

  6. 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?

Leave a Reply

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