Drag and Drop using DragLinearLayout in Android with Example
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.
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.
Now sync the project.
Step 3: Modify the strings.xml file
Below is the code for the strings.xml file.
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.
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.
Check out the project: https://github.com/ChaitanyaMunje/GFGImageSlider/tree/DragLinearLayout