Drag and Drop

Yugandhar
Android App Development for beginners
3 min readJul 25, 2016
  • we can allow your users to move data from one View to another View in the current layout using a graphical drag and drop gesture.
  • A drag and drop operation starts when the user makes some gesture that you recognize as a signal to start dragging data.
  • In response, your application tells the system that the drag is starting.

The drag/drop process

  • Started
  • Continuing
  • Dropped
  • Ended

Started: In response to the user’s gesture to begin a drag, your application calls startDrag() to tell the system to start a drag.

Continuing :The user continues the drag.

Dropped :The user releases the drag shadow within the bounding box of a View that can accept the data.

Ended :After the user releases the drag shadow, and after the system sends out (if necessary) a drag event with action type ACTION_DROP, the system sends out a drag event with action type ACTION_DRAG_ENDED to indicate that the drag operation is over.

The drag shadow

During a drag and drop operation, the system displays a image that the user drags. For data movement, this image represents the data being dragged. For other operations, the image represents some aspect of the drag operation.

layout.xml<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.compindia.dragdropapp.DropDropActivity">
<LinearLayout
android:id="@+id/ll_pinklayout"
android:orientation="vertical"
android:layout_height="@dimen/height_lldrop"
android:background="#FF8989"
>
<TextView
android:id="@+id/tv_dropdrop"
android:text="Drag Text"
android:textSize="@dimen/size_tvdrop"
/>
</LinearLayout>
</RelativeLayout>
activity.javapublic class DropDropActivity extends AppCompatActivity implements View.OnTouchListener, View.OnDragListener {
private String TAG = DropDropActivity.class.getSimpleName();
private LinearLayout llPinkLayout;
private TextView tvDragView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drop_drop);
setUpViews();
setListeners();
}
private void setListeners() {
tvDragView.setOnTouchListener(this);
llPinkLayout.setOnDragListener(this);
}
private void setUpViews() {
llPinkLayout = (LinearLayout) findViewById(R.id.ll_pinklayout);
tvDragView = (TextView) findViewById(R.id.tv_dropdrop);
}
@Override
public boolean onDrag(View view, DragEvent dragEvent) {
Log.d(TAG, "onDrag: view->" + view + "\n DragEvent" + dragEvent);
switch (dragEvent.getAction()) {
case DragEvent.ACTION_DRAG_ENDED:
Log.d(TAG, "onDrag: ACTION_DRAG_ENDED ");
return true;
case DragEvent.ACTION_DRAG_EXITED:
Log.d(TAG, "onDrag: ACTION_DRAG_EXITED");
return true;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(TAG, "onDrag: ACTION_DRAG_ENTERED");
return true;
case DragEvent.ACTION_DRAG_STARTED:
Log.d(TAG, "onDrag: ACTION_DRAG_STARTED");
return true;
case DragEvent.ACTION_DROP:
Log.d(TAG, "onDrag: ACTION_DROP");
View tvState = (View) dragEvent.getLocalState();
Log.d(TAG, "onDrag:viewX" + dragEvent.getX() + "viewY" + dragEvent.getY());
Log.d(TAG, "onDrag: Owner->" + tvState.getParent());
ViewGroup tvParent =(ViewGroup) tvState.getParent();
tvParent.removeView(tvState);
LinearLayout container = (LinearLayout)view;
container.addView(tvState);
LinearLayout contariner = (LinearLayout) view;
tvParent.removeView(tvState);
tvState.setX(dragEvent.getX());
tvState.setY(dragEvent.getY());
((LinearLayout) view).addView(tvState);
view.setVisibility(View.VISIBLE);
return true;
case DragEvent.ACTION_DRAG_LOCATION:
Log.d(TAG, "onDrag: ACTION_DRAG_LOCATION");
return true;
default:
return false;
}
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Log.d(TAG, "onTouch: view->view" + view + "\n MotionEvent" + motionEvent);
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
View.DragShadowBuilder dragShadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(null, dragShadowBuilder, view, 0);
return true;
} else {
return false;
}
}
}

#output

--

--