Loading...

Drag and Drop Android Example

Android provides a Drag and Drop framework, which lets the users to move data or view or layout from one position to another within the same android layout. This drag and drop framework includes drag listeners, drag event class, helper class and methods. In this android drag and drop tutorial, you will learn to implement simple vertical drag event in your android application. You can implement drag and drop functionality to any views and layouts like textview, imageview, button, imagebutton, linearlayout, relativelayout, framelayout, and more.

Here I have implemented drag and drop functionality to android TextView and like this way you can implement to ImageView and other android child views and group views.

Related:
Android GridView with Image and Text
Android ListView with Image and Text
Android CardView Example

Android Example: How to Implement Drag and Drop Functionality to Android App


Let’s start by creating a new android project to set drag and drop functionality in your android application with the project name: Android Drag and Drop Example and minimum SKD version: 15.

First open your project build.gradle file and add compile 'com.jmedeisis:draglinearlayout:1.1.0' as project dependencies and then sync the project. Build.gradle file will look like below.



XML Layout File

Open your application XML layout file and add com.jmedeisis.draglinearlayout.DragLinearLayout … layout and give it an id. Now inside this layout you can add any layouts and views for drag and drop event. Here I have added five/six TextView, you can add ImageView, Button and so on. Following is the complete content of XML layout file.

res/layout/activity_main.xml

Java Activity File

In your java activity file, link to the DragLinearLayout using findViewById method and set drag and drop functionality to the child inside that layout using “for loop” and by enabling draggable. Following is the complete code of java activity file.

src/MainActivity.java

Android Example: How to Implement Drag and Drop Functionality to Android App

Now run your Drag and Drop Android Example project by clicking run button. Drag the view top to bottom and bottom to top as vertically. You can implement drag and drop functionality by this way in your android app.
UI 8383045582174034818
Home item