How to get the absolute coordinates of a view in Android?

AndroidApps/ApplicationsMobile Development

Before getting into example, we should know what is absolute coordinates. It means absolute position (x,y)of a view on window manager. This example demonstrate about how to get the absolute coordinates of a view.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?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:padding = "16dp"
   tools:context = ".MainActivity"
   android:background = "#dde4dd">
   <TextView
      android:id = "@+id/text"
      android:layout_marginLeft = "100dp"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:text = "Hello World!" />
</RelativeLayout>

In the above xml, we have given one Textview. when user click on textview, it will show the position of the view on Toast.

Step 3 − Add the following code to src/MainActivity.java

package com.example.andy.myapplication;

import android.graphics.Point;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
   TextView textView;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      textView = findViewById(R.id.text);
      textView.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            int[] location = new int[2];
            textView.getLocationOnScreen(location);
            Toast.makeText(MainActivity.this,"X axis is "+location[0] +"and Y axis is "+location[1],Toast.LENGTH_LONG).show();
         }
      });
   }
   public static Point getLocationOnScreen(View view) {
      int[] location = new int[2];
      view.getLocationOnScreen(location);
      return new Point(location[0], location[1]);
   }
}

In the above code, when user click on textview, it will show absolute coordinates on the screen . Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

In the above result is initial screen, when user click on textview, it will show the result as shown below -

Click here to download the project code

raja
Published on 29-Jan-2019 17:24:20
Advertisements