Open In App

How to Get Touch Coordinates of Screen in Android?

Last Updated : 06 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Android, the app screen is a layout that can be used to display various UI elements like TextView, Button, ImageView, etc. These UI elements are positioned according to a set of pre-defined rules and developer changes. In simple words, each bit on any app screen defines a coordinate that can be used to track UI elements as well as user touch on the screen. This is pivotal wherein an application is a gaming software and requires the user to touch the screen to perform actions. So in this article, we will show you how you could fetch the coordinates X and Y of the screen touch in Android. Follow the below steps once the IDE is ready.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add two TextViews to display X and Y coordinates, one in each of them.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relative_layout_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <TextView
        android:id="@+id/text_view_1"
        android:layout_width="wrap_content"
        android:layout_height="60sp"
        android:layout_marginTop="50sp"
        android:layout_centerHorizontal="true"
        android:text="X:"
        android:textSize="50sp"/>
  
    <TextView
        android:id="@+id/text_view_2"
        android:layout_width="wrap_content"
        android:layout_height="60sp"
        android:layout_marginTop="30sp"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/text_view_1"
        android:text="Y:"
        android:textSize="50sp" />
  
</RelativeLayout>


Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package org.geeksforgeeks.touchcoordinates
  
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.RelativeLayout
import android.widget.TextView
  
class MainActivity : AppCompatActivity() {
    @SuppressLint("ClickableViewAccessibility", "SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Declaring and initializing the 
        // UI elements from the layout file
        val mRelativeLayout = findViewById<RelativeLayout>(R.id.relative_layout_1)
        val mTextViewX = findViewById<TextView>(R.id.text_view_1)
        val mTextViewY = findViewById<TextView>(R.id.text_view_2)
  
        // When relative layout is touched
        mRelativeLayout.setOnTouchListener { _, motionEvent ->
            
            // X and Y values are fetched
            val mX = motionEvent.x
            val mY = motionEvent.y
  
            // X and Y values are 
            // displayed in the TextView
            mTextViewX.text = "X: $mX"
            mTextViewY.text = "Y: $mY"
  
            true
        }
    }
}


Output:

You can see that X and Y coordinates are displayed in respective TextViews.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads