How to implement Drag Drop Widget in any android studio project
Today we will learn how to create android drag drop functionality using DragLinearLayout. Android DragLinearLayout library can be used in place of any LinearLayout. To do this we’ll add a third party library in our build.gradle file as follows:
implementation 'com.jmedeisis:draglinearlayout:1.1.0'
activity_main.xml
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text=" Example 1"
android:textSize="30dp"
android:textStyle="bold"
android:layout_marginTop="2dp"
android:background="@color/colorRed"/>
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="2dp"
android:text=" Example 2"
android:textSize="30dp"
android:textStyle="bold"
android:background="@android:color/holo_orange_dark"/>
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="2dp"
android:text=" Example 3"
android:textSize="30dp"
android:textStyle="bold"
android:background="@color/yellow"/>
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="2dp"
android:text=" Example 4"
android:textSize="30dp"
android:textStyle="bold"
android:background="@android:color/holo_blue_dark" />
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="2dp"
android:text=" Example 5"
android:textSize="30dp"
android:textStyle="bold"
android:background="@color/colorCyan" />
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="2dp"
android:text=" Example 6"
android:textSize="30dp"
android:textStyle="bold"
android:background="@color/colorPurple" />
android:id="@+id/textView7"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="2dp"
android:text=" Example 7"
android:textSize="30dp"
android:textStyle="bold"
android:background="@android:color/darker_gray" />
MainActivity.java
package com.friendvilla.simplecode;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import com.jmedeisis.draglinearlayout.DragLinearLayout;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DragLinearLayout dragLinearLayout = (DragLinearLayout) findViewById(R.id.container);
// set all children draggable except the first (the header)
for(int i = 0; i < dragLinearLayout.getChildCount(); i++){
View child = dragLinearLayout.getChildAt(i);
dragLinearLayout.setViewDraggable(child, child); // the child is its own drag handle
}
}
}
Tutorial/Demo Video: not Available