Code example for DragEvent

Methods: getAction, getClipData, getLocalState

0
 
	protected class MyDragEventListener implements View.OnDragListener {
 
		@Override 
		public boolean onDrag(View v, DragEvent event) {
			final int action = event.getAction();
 
			switch (action) {
			case DragEvent.ACTION_DRAG_STARTED:
				// All involved view accept ACTION_DRAG_STARTED for 
				// MIMETYPE_TEXT_PLAIN 
				if (event.getClipDescription().hasMimeType(
						ClipDescription.MIMETYPE_TEXT_PLAIN)) {
					commentMsg += ((Button) v).getText()
							+ " : ACTION_DRAG_STARTED accepted.\n"; 
					comments.setText(commentMsg);
					return true; // Accept 
				} else { 
					commentMsg += ((Button) v).getText()
							+ " : ACTION_DRAG_STARTED rejected.\n"; 
					comments.setText(commentMsg);
					return false; // reject 
				} 
			case DragEvent.ACTION_DRAG_ENTERED:
				commentMsg += ((Button) v).getText()
						+ " : ACTION_DRAG_ENTERED.\n"; 
				comments.setText(commentMsg);
				return true; 
			case DragEvent.ACTION_DRAG_LOCATION:
				commentMsg += ((Button) v).getText()
						+ " : ACTION_DRAG_LOCATION - " + event.getX() + " : "
						+ event.getY() + "\n";
				comments.setText(commentMsg);
				return true; 
			case DragEvent.ACTION_DRAG_EXITED:
				commentMsg += ((Button) v).getText()
						+ " : ACTION_DRAG_EXITED.\n"; 
				comments.setText(commentMsg);
				return true; 
			case DragEvent.ACTION_DROP:
				// Gets the item containing the dragged data 
				ClipData.Item item = event.getClipData().getItemAt(0);
 
				commentMsg += ((Button) v).getText() + " : ACTION_DROP"
						+ " - from " + item.getText().toString() + "\n";
				comments.setText(commentMsg);
 
				// If apply only if drop on buttonTarget 
				if (v == buttonTarget) {
					String droppedItem = item.getText().toString();
					commentMsg += ((Button) v).getText() + " : Dropped item - "
							+ droppedItem + "\n";
					comments.setText(commentMsg);
 
					// Retrieve the source view using getLocalState() 
					View dragView = (View) event.getLocalState();
 
					((Button) v).setBackgroundDrawable(((Button) dragView)
							.getBackground());
					((Button) v).setText(((Button) dragView).getText());
				}