Code example for DragEvent

Methods: getAction, getLocalState, getResult

0
		Drawable enterShape = getResources().getDrawable(
				R.drawable.shape_droptarget); 
		Drawable normalShape = getResources().getDrawable(R.drawable.shape);
 
		public boolean onDrag(View v, DragEvent event) {
			int dragAction = event.getAction();
			View dragView = (View) event.getLocalState();
 
			switch (event.getAction()) {
 
			case DragEvent.ACTION_DRAG_STARTED:
				// Do nothing 
				break; 
 
			case DragEvent.ACTION_DRAG_ENTERED:
				v.setBackgroundDrawable(enterShape);
				return true; 
				// v.setBackground(enterShape); 
 
			case DragEvent.ACTION_DRAG_EXITED:
				v.setBackgroundDrawable(normalShape);
				// v.setBackground(normalShape); 
				return false; 
 
			case DragEvent.ACTION_DROP:
				// Dropped, reassign View to ViewGroup 
 
				View view = (View) event.getLocalState();
				ViewGroup owner = (ViewGroup) view.getParent();
				owner.removeView(view);
				LinearLayout container = (LinearLayout) v;
				container.addView(view);
				view.setVisibility(View.VISIBLE);
				break; 
 
			case DragEvent.ACTION_DRAG_ENDED:
				// v.setBackgroundDrawable(normalShape); 
				v.setBackgroundDrawable(normalShape);
				// v.setBackground(normalShape); 
		         if (dropEventNotHandled(event)) {
		                dragView.setVisibility(View.VISIBLE);
		            } 
			default: 
				break; 
 
			case DragEvent.ACTION_DRAG_LOCATION:
				v.setVisibility(View.VISIBLE);
				break; 
			} 
			return true; 
		} 
 
	} 
	  private boolean dropEventNotHandled(DragEvent dragEvent) {
	        return !dragEvent.getResult();
	    } 
}