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”
Leave a Reply
You must be logged in to post a comment.
Its look simple thanks
nice example
Nice example. Thanks
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
Great example – works perfect – quick question: how do you do it with multiple images?
I've made a "real" drag and drop sample, which allows dragging of multiple custom views… Read all about it on my blog http://asimmittal.net/blog/?p=240
The above you have the link is not enter to the source code
nice
ohhhhhhhhhhhhhhhhhhhh
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
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.
its nice
Android has comprehensive drag and drop since API Level 1.
Check how pros do it here: http://developer.android.com/reference/android/view/View.html#dispatchDragEvent(android.view.DragEvent)
Developers should read more the dev guide.
DragEvent is included in API 11. Its not in API 1..
Sorry @XOR007, It's since API Level 11.
@XOR007 should read more the dev guide.
where is d code?
@Anonymous: There's a link at the bottom of the post. I think I will bold and highlight all those links from now on. 🙂
Nice example. Thanks
Please tell me how to drag and drop one ListView to another ListView ?
Nice example. Thanks
How to drag and drop from one ListView to another ListView?
please help me
goooooooood
I replaced Button to Imageview but it didnt work. Please help!
Dear please help me…
i want to drag and drop an "image" instead of a button which is showing here..
please help and if please help me with the code.
@Ahmed Khan: Whats the problem you are facing?
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.
This is too much of customization. You can use the newer APIs for this I guess. Else, you can start implementing it and see how far you can go.
I wanr to draw multiple circle shape and all circle should be draged and drop on the layout.can you help me regarding this.My id-dhananjay.kumar.cs@gmail.com
Hi sir,
If we have 2 images in a view or layout, can we drag a view or layout so that we can dag 2 images at a time. please help me out
awesome work dude.. thank u so much.. 🙂
can somebody post the code for dragging the button as a single image?
Good One…
how to avoid overlapping and how to redrag and redrop
Thankyou
I want to drag and drop the text drawn on custom view, plz help.
Its been so many days i am trying this.. plz provide the code.
i want to drag and drop the text drawn on custom view. plz help me, provide some sample code. its been many days i am trying to do this.
I want to use this to drag and drop images of a grid from one cell to another. Can anybody help me the changes I should do in order to make this successful.
nice example for drag and drop
Nice example for drag and drop. It is very useful to my application. thank u
Nice Example but how to use the image instead of button?
how to drop the button means redrop and reDrag?
Thank in advance
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
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.
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
Depends on your code. Are you extending a class? If so, which one?
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
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
After onMove, the button is removed, and what you see is just an image.
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. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (FrameLayout) findViewById(R.id.LinearLayout01);
// layout.setOnTouchListener(this);
ibtn = (ImageButton) findViewById(R.id.imageButton1);
ibtn.setDrawingCacheEnabled(true);
ibtn.setOnTouchListener(this);
iv1 = (ImageView) findViewById(R.id.imageView1);
iv1.setDrawingCacheEnabled(true);
iv1.setOnTouchListener(this);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}
public boolean onTouch(View view, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
status = START_DRAGGING;
image = new ImageView(this);
image.setImageBitmap(iv1.getDrawingCache());
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) {
System.out.println("Dragging");
image.setPadding((int) me.getRawX(), (int) me.getRawY(), 0, 0);
image.invalidate();
}
}
return false;
}
}
Wo ist der Fehler?
Sorry, wrote german. I tried this with a ImageView and Imagebutton. But it doesnt work. Where is my fault?
How it implement with gridview item.
Nice Example dear.It`s help me lot thank dear.
This is awesome example dear thanks for help me.
Nice….. Thanks…..
Hi,
unfortunatelly, I cannot open Source code page.
Could you please send me correct link, or code into email?
Thank you in advance.