Android Drag and Drop Textview Tutorial & Example

0
152

In this tutorial, we would be creating draggable textviews. These textviews would be dynamically created and then we would set their properties such that they can be dragged around the screen. You can also create the textviews in your xml file instead of dynamically creating them as we did in this tutorial. so follow along.

Checkout Source Code (Github)

Step 1: Create a new android project. File -> New -> New project. Enter the project name and click ok

Step 2: Open MainActivity.java and add the below code.

package com.example.movabletextview;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    String texts[] = {"C", "L", "A", "S", "S"};
    RelativeLayout layout;

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

        layout = (RelativeLayout) findViewById(R.id.linear);

        generateTv(texts);

    }

    private void generateTv(String texts[]) {

        int Xposition = 150;
        int Yposition = 500;


        for (int i = 0; i < texts.length; i++) {

            TextView tv = new TextView(this);
            tv.setText("" + texts[i]);
            tv.setX(Xposition);
            tv.setY(Yposition);
            tv.setTextColor(Color.RED);
            tv.setTextSize(33);
            tv.setOnTouchListener(mOnTouchListenerTv2);
            layout.addView(tv);
            Xposition += 100;

        }
    }


    public final View.OnTouchListener mOnTouchListenerTv2 = new View.OnTouchListener() {
        int prevX, prevY;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            final RelativeLayout.LayoutParams par = (RelativeLayout.LayoutParams) v.getLayoutParams();
            switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE: {
                    par.topMargin += (int) event.getRawY() - prevY;
                    prevY = (int) event.getRawY();
                    par.leftMargin += (int) event.getRawX() - prevX;
                    prevX = (int) event.getRawX();
                    v.setLayoutParams(par);
                    return true;
                }
                case MotionEvent.ACTION_UP: {
                    par.topMargin += (int) event.getRawY() - prevY;
                    par.leftMargin += (int) event.getRawX() - prevX;
                    v.setLayoutParams(par);
                    return true;
                }
                case MotionEvent.ACTION_DOWN: {
                    prevX = (int) event.getRawX();
                    prevY = (int) event.getRawY();
                    par.bottomMargin = -2 * v.getHeight();
                    par.rightMargin = -2 * v.getWidth();
                    v.setLayoutParams(par);
                    return true;
                }
            }
            return false;
        }
    };
}

 

Step 3: Open activity_main.xml and add the below code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

</RelativeLayout>

Thats all. You can run your app and start dragging the textviews.

LEAVE A REPLY

Please enter your comment!
Please enter your name here