Home » Android

How to get co-ordinates of touch points on screen in Android?



In this article, we are going to learn about co-ordinate of android device screen and use MotionEvent to get touch co-ordinates.
Submitted by Manu Jemini, on February 06, 2018

In the example below, we are going to use the View.MotionEvent class to get the coordinates of the x-axis and y-axis of the screen.

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 coordinates of the touch on the screen.

These coordinates 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 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) {
				final float x = event.getX();
				final float y = event.getY();

				float lastXAxis = x;
				float lastYAxis = y;

				E1.setText(Float.toString(lastXAxis));
				E2.setText(Float.toString(lastYAxis));

				return true;
			}
		});
	}

}

2) XMLfile:

<?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="Points 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="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"
        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

Android - Get co-ordinates 1
Android - Get co-ordinates 2

Android - Get co-ordinates 3






Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.