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
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.
ReplyDeleteWow!! 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.
ReplyDeleteHow 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 :)
ReplyDeleteWith 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.
ReplyDeleteHi 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.
ReplyDeleteThanks in andvace
I am having problem dragging and dropping images out from one layout to another layout. Anyone can help me?
ReplyDeleteNice 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
ReplyDeletehttps://www.youtube.com/watch?v=SYoN-OvdZ3M&index=19&list=PLonJJ3BVjZW6CtAMbJz1XD8ELUs1KXaTD
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.
ReplyDeleteIELTS 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
It was really an interesting blog, Thank you for providing unknown facts.
ReplyDeleteair hostess training in Bangalore
air hostess academy Bangalore
air hostess institute in Bangalore
best air hostess training institute in Bangalore
visit
ReplyDeletevisit
Awesome post with great piece of information. I really admire your article. Do share more.
ReplyDeleteUnix 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
Wonderful Article. its informative and useful for Android Development training.
ReplyDeleteAndroid Training in Bangalore
Android Training in kalyan Nagar
Very useful information, Keep posting more blog likes this, Thank you.
ReplyDeleteAviation 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
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.
ReplyDeletehonor service centres in chennai
honor service center velachery
honor service center in vadapalani
Very nice information. Thank you for sharing your knowledge. It is same as the information that i learned from one institute.
ReplyDeleteVery nice information. Thank you for sharing your knowledge with us. It is same as the information that i learned from one institute.
ReplyDeletePython Training in Kalyan nagar
Very nice posts. this could not be explained better. Thanks for sharing, Keep up the good work.
ReplyDeletePython training in kalyan nagar
Java Training in Kalyan Nagar
BluePrism training in kalyan nagar
Selenium Training in Kalyan Nagar
This comment has been removed by the author.
ReplyDeleteReally thanks for sharing such an useful info...
ReplyDeletesalesforce developer training
salesforce tutorials for beginners
192.168.1.1 IP Address. To access the admin page type 192.168.1.1 into your web browser's address bar or click on the link below.
ReplyDelete
ReplyDelete192.168.l.l is an IP address used by many routers and modems as the default gateway. 192.168.1.1 address can be used by the users to gain the router admin access and configure their routers and networks.
Excellent content information. Nice blog content.I also want to share some information about Signova Group
ReplyDelete