Android drag drop - DragLinearLayout With Examples

Today we will learn how to create android drag drop functionality using DragLinearLayout.

Android Drag Drop using DragLinearLayout

Android DragLinearLayout library can be used in place of any LinearLayout. To do this we’ll add a third party library in our build.gradle file as follows:

The above class library extends the LinearLayout and customises is to add the drag and drop feature.

onTouchEvent method is overridden to programmatically detect the gesture movements and drag and drop accordingly. We’ll get into details of the android drag and drop feature with more details in a later tutorial.

Note: You can view the complete source code of the DragLinearLayout layout in the libraries folder once the gradle is synced.

By default the child views inside the LinearLayout won’t be draggable. For that we’ll call the method
setViewDraggable(View, View) for each ChildView.

Android Drag Drop Example Project Structure

android-draggable-linearlayout-project

This android drag drop project consists of an activity and its layout along with a drawable image used in the layout. In this tutorial we’ve added the attributes of the UI widgets under styles.xml.

Android Drag Drop Code

The activity_main.xml code is given below:

The above layout consists of multiple TextViews and an ImageView. These are the ChildViews of the draggable layout.

The MainActivity.java is given below:

In the above code we’ve explicitly set each ChildView as a draggable one. The DraggableLinearLayout class present in the third party library contains the drag and drop implementation.

Note: This library comes with the limitation that it can only support Vertical Orientation of the LinearLayout.

The output below shows the app in action.
android drag drop example, android DragLinearLayout

This brings an end to android drag drop example. You can download the final Android DragLinearLayout Project from the below link.

Reference: GitHub Project

By admin

Leave a Reply