Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag and Drop in ConstraintLayout

Tags:

android

I am trying Drag and Drop in ConstraintLayout as below,

override fun onLongClick(v: View?): Boolean {
    val dragData = ClipData.newPlainText("", "")
    val myShadow = View.DragShadowBuilder(v)

    if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.N) {
        startDragAndDrop(dragData, myShadow, v, 0)
    }else{
        startDrag(dragData, myShadow, v, 0)
    }
    return true
}

And my drag listener is

private val mDragListen = View.OnDragListener { v, event ->
    val view = event.localState as TextView
    when (event.action) {
        DragEvent.ACTION_DRAG_STARTED -> {
            layoutParams = view.layoutParams as ConstraintLayout.LayoutParams?
            true
        }
        DragEvent.ACTION_DRAG_ENTERED -> {
            true
        }

        DragEvent.ACTION_DRAG_LOCATION ->
            true
        DragEvent.ACTION_DRAG_EXITED -> {
            true
        }
        DragEvent.ACTION_DROP -> {
            layoutParams?.marginStart = event.x.toInt()
            layoutParams?.topMargin = event.y.toInt()
            view.layoutParams = layoutParams
            view.invalidate()
            true
        }

        DragEvent.ACTION_DRAG_ENDED -> {
            when (event.result) {
                true ->
                    Toast.makeText(activity, "The drop was handled.", Toast.LENGTH_LONG)
                else ->
                    Toast.makeText(activity, "The drop didn't work.", Toast.LENGTH_LONG)
            }.show()
            true
        }
        else -> {
            Log.e("DragDrop Example", "Unknown action type received by OnDragListener.")
            false
        }
    }
}

Here is my xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/clContainer"
    android:layout_height="match_parent"
    tools:context="com.guna.kotlinapplication.BlankFragment">

    <com.guna.kotlinapplication.DraggableTextView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="183dp"
        android:layout_marginStart="56dp"
        android:paddingStart="5dp"
        android:paddingLeft="5dp"
        android:text="@string/app_name"
        android:paddingEnd="5dp"
        android:tag="@string/app_name"
        android:textSize="22sp"
        android:paddingRight="5dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:src="@drawable/ic_launcher_background" />
</android.support.constraint.ConstraintLayout>

This is working, but not as expected, see the output.

enter image description here

like image 396
Gunaseelan Avatar asked Oct 09 '18 11:10

Gunaseelan


1 Answers

The DragEvent methods getX() and getY() return the coordinates of the drag point as pointed here https://developer.android.com/reference/android/view/DragEvent.html#getX()

In your case just subtract half the view height and width to achieve the expected result

layoutParams?.marginStart = event.x.toInt() - (view.width.div(2))
layoutParams?.topMargin = event.y.toInt() - (view.height.div(2))
like image 159
rm-rf Avatar answered Oct 03 '22 22:10

rm-rf