C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Android

How to get motion touch co-ordinates in Android?

In this article, we are going to learn about motion touch and then we will try to get co-ordinates between two end points of a line.
Submitted by Manu Jemini, on February 08, 2018

In the example below, we are going to use the View.MotionEvent class to get the co-ordinates of the x-axis and y-axis of the MotionTouchEvent on a particular line.

To implement this in your program you will need to import: android.view.MotionEvent in your java file.

Then create a layout file with Two EditText and a single TextView like this:

<EditText
        android:layout_width="400dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:hint="Points on X-Axis" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText2"
        android:hint="Points on Y-Axis"
        android:layout_alignRight="@+id/editText"
        android:layout_alignEnd="@+id/editText" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Touch to get co-ordinates"
        android:id="@+id/textview"
        android:focusable="true" />

First, we will take the reference of the TextView and EditTexts from the layout file, then set an onTouchEventLisetener on TextView.

Then we will override the function onTouch() which will have an argument MotionEvent which contains the co-ordinates of the touch on the screen.

These co-ordinates of the x-axis and y-axis will then be set on the EditText and we will have the result every time we touch the screen.

1) Java file:

package com.example.hp.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity
{
    TextView T;
    EditText E1, E2;
    float lx=0f;
    float ly=0f;
    float x = 0f;
    float y = 0f;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        E1 = (EditText) findViewById(R.id.editText);
        E2 = (EditText) findViewById(R.id.editText2);
        T=(TextView)findViewById(R.id.textview);
        T.setOnTouchListener(new View.OnTouchListener(){


            @Override
            public boolean onTouch(View v, MotionEvent event) {
                 float x = event.getX();
                 float y = event.getY();

                 float nx = x - lx;
                 float ny = y - ly;

                x += nx;
                y += ny;

                E1.setText(Float.toString(x));
                E2.setText(Float.toString(y));

                return true;
            }
        });
    }
}

2) XML file:

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hp.myapplication.MainActivity">

    <EditText
        android:layout_width="400dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:hint="Moving co-ordinates on X-Axis" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText2"
        android:layout_below="@+id/editText"
        android:layout_alignLeft="@+id/editText"
        android:layout_alignStart="@+id/editText"
        android:hint="Moving co-ordinates on Y-Axis"
        android:layout_alignRight="@+id/editText"
        android:layout_alignEnd="@+id/editText" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Touch here and move a little"
        android:id="@+id/textview"
        android:focusable="true"
        android:clickable="true"
        android:textSize="20dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

3) Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output

get motion touch co-ordinates in Android 1
get motion touch co-ordinates in Android 2



Liked this article? Do share with your friends :)


COMMENTS