Drag and Drop using DragLinearLayout in Android with Example
Last Updated :
26 Feb, 2025
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.
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:android="http://schemas.android.com/apk/res/android"
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.
MainActivity.java:
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
Similar Reads
Android Drag and Drop with Kotlin
In most Android applications where data is displayed in the form of a list format, there is a functionality with the help of which we can simply drag and drop any item from that list to any specific position within the list. To implement this type of drag-and-drop functionality we have to use a Drag
5 min read
CustomArrayAdapter in Android with Example
In Android, ArrayAdapters are used for populating and controlling the ListView and Spinners. ArrayAdapter by default provides List-Items that include only single information or single TextView. In order to have a more complex layout that includes multiple information in a single List-Item such as im
6 min read
Auto Image Slider in Android with Example
Auto Image Slider is one of the most seen UI components in Android. This type of slider is mostly seen inside the apps of big E-commerce sites such as Flipkart, Amazon, and many more. Auto Image Slider is used to represent data in the form of slides that change after a specific interval of time. In
6 min read
How to Use Drag-and-Drop in Android Apps?
You may utilize the Android drag/drop framework to enable your users to move data with a graphical drag and drop action. This can be from one View in your own program to another, or between your app and another when the multi-window mode is enabled. A drag event class, drag listeners, and auxiliary
7 min read
Context Menu in Android with Example
In Android, there are three types of menus available to define a set of options and actions in the Android apps. Here in this article let's discuss the detail of the Context Menu. In Android, the context menu is like a floating menu and arises when the user has long-pressed or clicked on an item and
4 min read
LinearLayout and its Important Attributes with Examples in Android
LinearLayout is one of the most basic layouts in android studio, that arranges multiple sub-views (UI elements) sequentially in a single direction i.e. horizontal or vertical manner by specifying the android:orientation attribute. If one applies android:orientation="vertical" then elements will be a
3 min read
Theming of Material Design EditText in Android with Example
Prerequisite: Material Design EditText in Android with Examples In the previous article Material Design EditText in Android with Examples Material design Text Fields offers more customization and makes the user experience better than the normal text fields. For example, Icon to clear the entire Edit
4 min read
SlidingDrawer in Android with Example
SlidingDrawer is a common requirement in android mobiles to view the content whenever needed by sliding horizontally as well as vertically. In this article, let us check out Sliding Drawer which is used to hide the content out from the screen and allows the user to drag a handle to bring the content
9 min read
How to Create a Simple Lucky Draw Spinning Wheel in Android?
Pre-requisites: Android App Development Fundamentals for BeginnersGuide to Install and Set up Android StudioHow to Create/Start a New Project in Android Studio?Running your first Android appCountDownTimer in Android with ExampleRandom Class in Java We are going to build a lucky draw spinning wheel,
4 min read
Shared Element Transition in Android with Example
Shared Element Transition is one of the most seen animations in Android apps. This type of animation is used when we have to open an item from a ListView or RecyclerView. Shared Element Transition in Android determines how shared element views are animated from activity to activity or fragment to fr
4 min read