Moving object with touch events | Android Studio | Kotlin

How to move object/view with touch events on the screen?

We can easily move any Object/View (ImageView, TextView, Button, etc.) on the screen using the Touch feature. In this tutorial, we will add an ImageView in our activity layout and will move that ImageView on screen.
>>Watch For Java

Step 1: Create a new project or open an existing project

Step 2: Code

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relative_layout"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Drag &amp; Drop"
        android:textColor="@color/colorPrimary"
        android:textSize="30sp"
        android:textStyle="bold" />


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:adjustViewBounds="true"
        android:src="@drawable/logo" />

</RelativeLayout>
MainActivty.kt
package com.blogspot.atifsoftwares.dragdrop

import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.View.OnTouchListener
import android.widget.RelativeLayout
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    
    private var xDelta = 0
    private var yDelta = 0
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //setup layout params
        val layoutParams = RelativeLayout.LayoutParams(800, 600)
        imageView.layoutParams = layoutParams
        //setup touch listener
        imageView.setOnTouchListener(CustomTouchListener())
    }

    private inner class CustomTouchListener : OnTouchListener {
        override fun onTouch(v: View, event: MotionEvent): Boolean {
            val x = event.rawX.toInt()
            val y = event.rawY.toInt()
            when (event.action and MotionEvent.ACTION_MASK) {
                MotionEvent.ACTION_DOWN -> {
                    val lParams = v.layoutParams as RelativeLayout.LayoutParams
                    xDelta = x - lParams.leftMargin
                    yDelta = y - lParams.topMargin
                }
                MotionEvent.ACTION_UP -> Toast.makeText(this@MainActivity, "Image is on new Location!", Toast.LENGTH_SHORT).show()
                MotionEvent.ACTION_POINTER_DOWN -> {
                }
                MotionEvent.ACTION_POINTER_UP -> {
                }
                MotionEvent.ACTION_MOVE -> {
                    val layoutParams = v.layoutParams as RelativeLayout.LayoutParams
                    layoutParams.leftMargin = x - xDelta
                    layoutParams.topMargin = y - yDelta
                    layoutParams.rightMargin = 0
                    layoutParams.bottomMargin = 0
                    v.layoutParams = layoutParams
                }
            }
            relative_layout.invalidate()
            return true
        }
    }
    
}

Step 3: Run Project

Output

Moving object with touch events | Android Studio | Kotlin


Comments

Popular posts from this blog

Picture In Picture | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Kotlin

Add a Back Button to Action Bar Android Studio (Kotlin)