Open In App
Related Articles

Drag and Drop using DragLinearLayout in Android with Example

Improve Article
Save Article
Like Article

In most of the To-do list apps, we need to create a view such that the user can prioritize his daily tasks according to his priority. For this feature to work he should be able to drag and drop view items. For adding this type of functionality inside our app we have to use DragLinearLayout inside our app. In this article, we will take a look at How we can add Drag and Drop functionality for each view to change the view position. For this feature, we have to add a library for DragLinearLayout. 

Implementation of DragLinearLayout Library

Using DragLinearLayout we can create a parent view inside which we can make our child item draggable. A sample GIF is given below from which we can get an idea of what we are going to do in this article. Note that we are going to implement this project using the Java language. 

Drag and Drop using DragLinearLayout

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Make sure to select Java as the programming language.

Step 2: Add dependency to build.gradle(Module:app)

Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section. 

implementation ‘com.jmedeisis:draglinearlayout:1.1.0’

Now sync the project.

Step 3: Modify the strings.xml file

Below is the code for the strings.xml file.


    <string name="app_name">GFG App</string>
    <string name="image_desc">image</string>
    <string name="dsa_course">DSA Course</string>
    <string name="geeks_for_geeks">Geeks for Geeks</string>

Step 4: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.  


<?xml version="1.0" encoding="utf-8"?>
    <!--Parent layout is a Draglinearlayout
        whose orientation is vertical and
        we have to provide id to it.-->
    <!--below is a simple image view -->
        android:src="@drawable/gfgimage" />
    <!--below is the simple text view-->
        android:textStyle="bold" />
    <!--below is the simple text view-->
        android:textStyle="bold" />

Step 5: Working with the file

Navigate to the app > java > your apps package name > file and open file. Below is the code for the file. Comments are added inside the code to understand the code in more detail. 


import android.os.Bundle;
import android.view.View;
import com.jmedeisis.draglinearlayout.DragLinearLayout;
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        // below lines is to initialize our Drag linear layout
        DragLinearLayout dragLayout = findViewById(;
        // we are creating for loop for dragging 
        // and dropping of child items.
        for (int i = 0; i < dragLayout.getChildCount(); i++) {
            // below is the child inside dragger layout
            View child = dragLayout.getChildAt(i);
            // below line will set all children draggable
              // except the header layout.
              // the child is its own drag handle.
            dragLayout.setViewDraggable(child, child);


Check out the project:

Last Updated : 23 Dec, 2020
Like Article
Save Article
Similar Reads
Related Tutorials