Code example for DragEvent

Methods: getAction, getLocalState, getResult

0
        Drawable normalShape = getResources().getDrawable(R.drawable.shape);
 
        @Override 
        public boolean onDrag(View v, DragEvent event) {
        	FrameLayout container = null;
            View view = (View) event.getLocalState();
        	ViewGroup owner = null;
        	 
            switch (event.getAction()) {
             
            case DragEvent.ACTION_DRAG_STARTED:
            	try { 
	            	container = (FrameLayout) v;
	            	if (container.getChildCount() < 1) {
	                    v.setBackgroundDrawable(validDropShape);
	            		return true; 
	            	} 
            	} 
            	catch (Exception e) {
            		Log.e("MainActivity", "ACTION DRAG STARTED failed", e);
            	} 
        		return false; 
             
            case DragEvent.ACTION_DRAG_ENTERED:
                v.setBackgroundDrawable(enterShape);
                break; 
 
            case DragEvent.ACTION_DRAG_LOCATION:
                // Ignore the event 
            	return true; 
             
            case DragEvent.ACTION_DRAG_EXITED:
                v.setBackgroundDrawable(validDropShape);
                break; 
             
            case DragEvent.ACTION_DROP:
                // Dropped, reassign View to ViewGroup 
                owner = (ViewGroup) view.getParent();
                owner.removeView(view);
                container = (FrameLayout) v;
                container.addView(view);
                Toast.makeText(GameActivity.this, "The drop was handled.", Toast.LENGTH_SHORT).show();
                break; 
                 
            case DragEvent.ACTION_DRAG_ENDED:
                v.setBackgroundDrawable(normalShape);
                 
                if (!event.getResult()) {
                	Toast.makeText(GameActivity.this, "The drop didn't work.", Toast.LENGTH_SHORT).show();
                }; 
                break; 
                 
            default: