Android drag drop DragLinearLayout



Let's Start with Drag n Drap Functionality in Android Layout


Today we will learn how to create android drag drop functionality using DragLinearLayout.

Android Drag Drop 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:
compile 'com.jmedeisis:draglinearlayout:1.1.0'

The above class library extends the LinearLayout and customises is to add the drag and drop feature.

onTouchEvent method overrides to programmatically detect motion gestures and drag respectively. We'll get into the details of the android drag and drop feature in more detail in a later tutorial.

Note: You can view the complete source code of the DragLinearLayout layout in the libraries folder once the gradle is synced.

By default the child views inside the LinearLayout won’t be draggable. For that we’ll call the method
setViewDraggable(View, View) for each ChildView.

Android Drag Drop Example Project Structure


This android drag drop project consists of an activity and its layout along with a drawable image used in the layout. In this tutorial we’ve added the attributes of the UI widgets under styles.xml.

Android Drag Drop Code


The activity_main.xml code is given below:
<com.jmedeisis.draglinearlayout.DragLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:text="Please Drag and Drop Me Somewhere"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/TextViewStyle" />

<TextView
android:background="@android:color/holo_red_light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/TextViewStyle" />

<TextView
android:text="This tutorial uses a third party library to drag LinearLayouts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/TextViewStyle" />

<ImageView
android:layout_width="match_parent"
android:layout_height="120dp"
android:scaleType="centerCrop"
android:src="@drawable/img_1"
/>

<TextView
android:text="Ever thought how dragging songs in an application playlist works!"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/TextViewStyle" />

</com.jmedeisis.draglinearlayout.DragLinearLayout>

The above layout consists of multiple TextViews and an ImageView. These are the ChildViews of the draggable layout.

The MainActivity.java is given below:
package com.journaldev.draggablelinearlayout;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.jmedeisis.draglinearlayout.DragLinearLayout;

public class MainActivity extends AppCompatActivity {

@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
}
}
}

In the above code, we define each ChildView as portable. The DraggableLinearLayout class, presented in a third-party library, includes the implementation of the pull-out and release.

Note: This library comes with a limitation that can only help LinearLayout vertical orientation.

The output below shows the app in action.
android drag drop example, android DragLinearLayout

This brings an end to android drag drop example. You can download the final Android DragLinearLayout Project from the below link.

Reference: GitHub Project

Contributer

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google

0 comments:

Hot-product

Best Watch Store

Popular Posts

Contact Form

Name

Email *

Message *