Open In App

Drag and Drop using DragLinearLayout in Android with Example

Last Updated : 23 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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.

XML




<resources>
    <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>
</resources>


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




<?xml version="1.0" encoding="utf-8"?>
<com.jmedeisis.draglinearlayout.DragLinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <!--Parent layout is a Draglinearlayout
        whose orientation is vertical and
        we have to provide id to it.-->
  
    <!--below is a simple image view -->
    <ImageView
        android:id="@+id/idIVlogo"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:contentDescription="@string/image_desc"
        android:src="@drawable/gfgimage" />
  
    <!--below is the simple text view-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/purple_500"
        android:padding="20dp"
        android:text="@string/geeks_for_geeks"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:textStyle="bold" />
  
    <!--below is the simple text view-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/purple_500"
        android:padding="10dp"
        android:text="@string/dsa_course"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:textStyle="bold" />
      
</com.jmedeisis.draglinearlayout.DragLinearLayout>


Step 5: Working with the MainActivity.java file

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

Java




import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import com.jmedeisis.draglinearlayout.DragLinearLayout;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // below lines is to initialize our Drag linear layout
        DragLinearLayout dragLayout = findViewById(R.id.container);
  
        // 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);
        }
    }
}


Output: 

Check out the project: https://github.com/ChaitanyaMunje/GFGImageSlider/tree/DragLinearLayout



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads