Moving object with touch events | Android Studio | Java

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.

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>

MainActivity.java
package com.blogspot.atifsoftwares.dragdrop;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //declare ui views
    private ImageView imageView;
    private RelativeLayout relativeLayout;

    private int xDelta;
    private int yDelta;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //init ui views
        imageView = findViewById(R.id.imageView);
        relativeLayout = findViewById(R.id.relative_layout);

        //setup layout params
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(800, 600);
        imageView.setLayoutParams(layoutParams);
        //setup touch listener
        imageView.setOnTouchListener(new CustomTouchListener());

    }

    private class CustomTouchListener implements View.OnTouchListener {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            final int X = (int) event.getRawX();
            final int Y = (int) event.getRawY();

            switch (event.getAction() & MotionEvent.ACTION_MASK) {

                case MotionEvent.ACTION_DOWN:
                    RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) v.getLayoutParams();

                    xDelta = X - lParams.leftMargin;
                    yDelta = Y - lParams.topMargin;

                    break;

                case MotionEvent.ACTION_UP:
                    Toast.makeText(MainActivity.this, "Image is on new Location!", Toast.LENGTH_SHORT).show();
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
                    layoutParams.leftMargin = X - xDelta;
                    layoutParams.topMargin = Y - yDelta;
                    layoutParams.rightMargin = 0;
                    layoutParams.bottomMargin = 0;
                    v.setLayoutParams(layoutParams);
                    break;

            }
            relativeLayout.invalidate();
            return true;
        }
    }

}

Step 3: Run Project

Output

Moving object with touch events | Android Studio | Java
Moving object with touch events | Android Studio | Java


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)