Android Question Drag and Drop

Brad Henderson

Member
Licensed User
Hello All,

Hoping someone can point towards a solution. I want to Drag and Drop a label around the screen and find its x,y co-ordinates at the start and at the end.

I have been trying GestureDetector v2.4 but cannot find any instructions for "Drag". Is this a Drag and Drop function or should I be using Gesture_onTouch to find the start point and something like Gesture_onFling to calculate the end point? Then I can use these values to "relocate" (Drop) the label to the drop Zone.

Your help is appreciated.

Brad
 

Brian Dean

Well-Known Member
Licensed User
Longtime User
If you look here you might find something that helps you solve your problem. Try the demo in post #2.

EDIT : Here is the demo from that post, quickly converted to B4A (not B4X, I am afraid) and modified to drag labels rather than buttons. You will need to adapt it further if you simply want to get the label coordinates at the end of the drag operation, but that would be an easy task.
 

Attachments

  • DragDrop.zip
    10.5 KB · Views: 103
Last edited:
Upvote 0

Brad Henderson

Member
Licensed User
Hello Brian,

That demo is excellent. So much is happening in such little code. I am working through it trying to see if I can understand the processes and how I might adapt to my exact needs. If I could impose on your generosity once more, I would like to move a label (call it John) from the starting panel1 to another panel2 on the screen and if there exists a label (Call it Bill) in panel2 then replace it with the John from panel1 and in Panel1 replace John with Bill. So essentially swapping Bill with John.
I'll keep breaking down the code to better understand the workings of your demo. Would you suggest that I create another two panels?

Thanks Again for your time.
Brad
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Yes - I would be happy to help, but not today unfortunately as I am committed to family affairs. Meanwhile here are some notes that might help you sort things out for yourself.

Note that it is the label contents that are moved, not the labels themselves. The touch-down event identifies the source label; the touch-up identifies the destination label; then it is the contents that are exchanged. The proxy label is used to animate the drag movement and lives on the overlying touch panel.

If you are using two panels then then they will need to be covered by a single overlay touch panel. That will not be a problem if the base panels are adjacent, but would be a problem if there are intervening interactive views (buttons, text entry etc) as the touch panel would intercept any user actions on them.

If your labels are not in a grid, or are on different panels, then that does not matter - put them in a list and scan the list to find which view contains the touch position. This will be a little more complicated because you will have to take accouunt of the label's parent panel as well.
 
Last edited:
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Okay - I have produced a modified demo - a bit of a hasty effort for reasons that you will appreciate, I am sure. This version drags labels between two panels. I have optimised the layout a little for reasons of development speed - the panels are adjacent and conveniently located [Edit : . . . but see post #8 below]. It is unlikely that this solution completely fits your current app design but hopefully it will give you enough information and ideas to move forward. If not come back to me, but I will need to see a mockup of your layout before I can do anything more useful.

Good luck.

Edit : Demo file updated to include fuller comments and tidy up some loose ends - 23:00 Jul 8th 2024
 

Attachments

  • DragDrop2.zip
    11.2 KB · Views: 94
Last edited:
Upvote 0

Brad Henderson

Member
Licensed User
Once again Brian.,

Thank you for your help. This is what I was trying to achieve. It will only require minor adjustment to suit. This is excellent.
Thank you for the descriptions they will help me break down what is happening.

Brad
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I have modified the demo originally posted in post #6. I had aligned the overlaid touch panel with the screen origin to simplify the code and save a bit of development time. In fact this was not necessary; only two lines of code are needed to adapt it to work with an overlay panel that is not so fixed. In the updated demo it is necessary only that the overlay panel entirely covers the two working panels.
 
Upvote 0
Top