Android drag & drop

512 views Asked by At

Before that I start explaining the problem I need to say one thing: I'm totally new to android programming, I can use java without problem, but i still don't know the android packages and classes. I have a LinearLayout with 2 columns, inside the first column there are some buttons, the second contains a scrollView, in this scrollViewthere should be some... items that the user can add, those items have a title and a text, i already defined thoose objects in another class, and i add them to the scrollViewfrom a button. From now i put in that object a button that is used to delete the item, but now i realized that it's really ugly, so i was thinking: is it possible to allow the user to drag and drop the item out of the screen to delete it? I could surely do it by managing the event of pression and release on the item with some listener, but i'm almost sure that there's some android class created for sliding UI components. Something like what you do with android notification.

I post an image of how the current app is so you can understand better what i said: https://i.stack.imgur.com/fHWK3.jpg (I don't have enough reputation to post images)

If i wasn't clear just say and i will edit with the requested information.

UPDATE: After reading the resource that Bonatti posted in his answer i added a OnLongClickListener for starting the drop, and a OnDragListener to look the cordinates during the drag, now i have a problem... I need my component to be dragged only horizzontally like how you usually remove notification from the notification bar, i thought about the fact that i could save the Y-location of the shadow created by the DragShadownBuilder when i create it, and then i could check in the OnDrag event if the Y-location now is different from the beginning i correct it, but the problem is... how can i access the shadow's location?

1

There are 1 answers

1
Bonatti On BEST ANSWER

The basics of what you are asking for can be found here: I cant post more than 2 links, so redo these: http:// developer.android.com/guide/topics/ui/ drag-drop.html

These other might help with what you need in details: Link 1: code.tutplus Or Link 2: vogella They both answer what you need.

To put it simple: You need an "action" to receive that a button/something was pressed (and that is still being pressed), and then update the screen to match the item position