I’ve been working on Android projects for the past few weeks. For oneof them I wanted some drag-and-drop functionality that is sadlymissing from versions of Android prior to 3.0. I had some time on myhands so decided to create a library that mimics the Honeycombdrag-and-drop API.
As I couldn’t retrofit the drag-and-drop functionality in to the coreView
class, I created a DragArea
class to handle visualisation andevent dispatch for the drag operation. All Views
wishing to receivedrag events, or start a drag must be children of a DragArea
. TheDragArea
class itself is based on a FrameLayout
so is very easy toadd in to the widget hierarchy.
Starting a drag is very similar to the Honeycomb API. A View
wishingto start a drag operation must have a reference to the DragArea
andprovide some clip data to transfer along with an object used to drawthe drag visualisation.
Bundle data = new Bundle();data.putCharSequence("cliptext", "Some clip data goes in this bundle");dragArea.startDrag(data, new ViewDragShadowBuilder(this);
As methods for receiving drag events are not built in to the View
API, views wishing to receive drag events must first register forthem. The DragEvent
class itself is very similar to Honeycomb.
dragArea.addDragListener(this, new OnDragListener { @Override public void onDrag(View view, DragEvent dragEvent) { switch dragEvent.getAction() { case DragEvent.ACTION_DRAG_STARTED: break; case DragEvent.ACTION_DRAG_ENTERED: break; case DragEvent.ACTION_DRAG_EXITED: break; case DragEvent.ACTION_DROP: Bundle data = dragEvent.getBundle(); CharSequence dropText = data.getCharSequence("cliptext"); reportView.setText(dropText); case DragEvent.ACTION_DRAG_ENDED: default: break; } }});
The code, as an android library project, is available ongithub as well as thedocumentation. A fullexample applicationthat makes use of the library is also available.
I know that this work becomes pointless when most android phones areupdated to Ice Cream, but until then it might be useful to someone whoneeds drag-and-drop on android 2.0. The similarity to the official APImight make it easier to port code in the future.
This is a nice little library that would be of use to me, but you don’t include any copyright notice or license file in the github repository. What licensing terms do you intend?