Thursday, July 29, 2010

implement drag and drop on android

Nowadays, drag and drop is a frequently seen feature on touch screen devices. Tthis post introduces the basic idea of how to implement drag and drop on android.

On android, touch event is composed of a series events.
First, user puts his finger on an element, and the element receives a ACTION_DOWN event.
Then, while holding finger on screen, user moves his finger to a new location. The element receives a series of ACTION_MOVE events.
Finally, user raises his finger. At this point, the element receives ACTION_UP event.
Consider the figure below, the parent element has a child element inside. User can drag the child element anywhere in parent.

There are two important traits of touch event on android.
First, touch event will be propagated. That is, if a child chooses to ignore the first event (ACTION_DOWN) by returning false in its onTouchEvent handler, the parent's onTouchEvent handler will receive the event. Unless one of the ancestors agrees to handle the event or the root is reached, the event will continually be propagated.
Second, parent can intercept the touch event before its child's onTouchEvent handler is fired. This is achieved by overriding the onInterceptTouchEvent method on parent, and returning true from it. As a consequence, the child's onTouchEvent handler will be bypassed, and the parent's onTouchEvent handler will fire.
The work flow is shown in below diagram:


We need to setup the onTouchEvent handler for both child and parent. In child's handler, we save the child element as the item to be dragged, and return false so that subsequent event will be delivered to parent's handler. In parent's handler, we change the child's margin to match the position of the finger, so the child will follow our finger.

Sample code:
http://code.google.com/p/rxwen-blog-stuff/source/browse/#svn/trunk/android/drag

Reference:
http://developer.android.com/guide/topics/ui/ui-events.html

17 comments:

Anonymous said...

this is not a good way to implement. I tried and found that child will no longer get further events once return false (and after ACTION_UP). the source code is very easy to understand on this issue. what i have done is to always allow child to handle the drag and zoom. it works very well without parent involved.

SCC said...

Wow!! You two are way to smart. I'm just trying to learn how to drag and drop with my android 2.2 Lg touchscreen phone. And after reading this I'm more lost than ever! Lol. Like I said you two are way to smart on these things to be posting on one page. You both should be teaching this stuff or atleast getting paid big money somewhere.

Anonymous said...

How about if there are two or more images, how will you implement this? Please help... I'm new to programming and android. Thanks in advance :)

Anonymous said...

With more images to drag and drop, you need to determine which image the user has selected by getting the coordinates from the ACTION_DOWN event.

ragupathi said...

Hi im new one for android. im develop now drag and drop its very useful to me,But i need to shift the views like when i touch one view to drag and drop to another view these two views can be exchange the position. if any idea please share with me. one more thing i dont need onTouch listener instead of i need onLongClicklistner.

Thanks in andvace

christina said...

I am having problem dragging and dropping images out from one layout to another layout. Anyone can help me?

Vivek Ramesh said...

Nice post, but here is a video that explains the relationship between the dispatchTouchEvent, the onInterceptTouchEvent and the onTouchEvent with the help of a custom layout and a custom View inside that layout
https://www.youtube.com/watch?v=SYoN-OvdZ3M&index=19&list=PLonJJ3BVjZW6CtAMbJz1XD8ELUs1KXaTD

Pavithra M said...

It is really a great work and the way in which u r sharing the knowledge is excellent.
Thanks for helping me to understand basic concepts. As a beginner in android programming your post help me a lot.Thanks for your informative article. Android Training in velachery | Android Training institute in chennai

vigneswaran said...

It's really interesting I'll be read that many of the bloggers relevant android development that's time I read that's your tips helped me and clarify the new thing. pretty explicit helpful content.
.Net Training in Chennai
.Net Training Institute in Chennai
Hadoop Training Institute in Chennai
Best Selenium Training in Chennai

Anbarasan14 said...

I believe that your blog will surely help the readers who are really in need of this vital piece of information. Waiting for your updates.

IELTS Classes in Mumbai
IELTS Coaching in Mumbai
Best IELTS Coaching in Mumbai
IELTS Center in Mumbai
IELTS Training in Mumbai
IELTS Coaching Classes in Mumbai
Best IELTS Course near me

keerthana said...

Very nice blog, Thank you for providing good information.
Aviation Academy in Chennai
Aviation Courses in Chennai
aviation institute in chennai
best aviation academy in chennai

jenifer irene said...

It was really an interesting blog, Thank you for providing unknown facts.
air hostess training in Bangalore
air hostess academy Bangalore
air hostess institute in Bangalore
best air hostess training institute in Bangalore

somar said...

visit
visit

Praylin S said...

Awesome post with great piece of information. I really admire your article. Do share more.
Unix Training in Chennai
Unix Shell Scripting Training in Chennai
Embedded System Course Chennai
Embedded Training in Chennai
LINUX Training in Chennai
LINUX Course in Chennai
Unix Training in Porur
Unix Training in OMR

Deepthi said...

Wonderful Article. its informative and useful for Android Development training.


Android Training in Bangalore
Android Training in kalyan Nagar

ajay prakash said...

Very useful information, Keep posting more blog likes this, Thank you.
Aviation Academy in Chennai
Air hostess training in Chennai
Airport management courses in Chennai
Ground staff training in Chennai
best aviation academy in chennai
Air Hostess Training Institute in chennai
Airline Courses in Chennai
airport ground staff training in chennai


service care said...

Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts.
honor service centres in chennai
honor service center velachery
honor service center in vadapalani