Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the View location (position) on display (screen) in android

I want to find the view's position on the display screen.

To do it, I use the methods such as view.getLeft() ,view.getBottom() , view.getRight() , view.getTop(). But unfortunately, all these methods are returned 0.
Is there any alternate way to find the view's positions (i.e coordinates on screen)?

My layout main.xml:

<TextView android:id="@+id/tv1"
    android:layout_width="200px"
    android:layout_height="30px"
    android:text="welcome to" />

<TextView android:id="@+id/tv2"
    android:layout_width="200px"
    android:layout_height="30px"
    android:text=" Make Eit solution " />

And my class has this in onCreate():

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);

System.out.println("tv4 width:"+tv2.getWidth());
System.out.println("tv4 height:"+tv2.getHeight());
System.out.println("Right:"+tv2.getRight());
System.out.println("Left:"+tv2.getLeft());
System.out.println("Top:"+tv2.getTop());
System.out.println("Bottom:"+tv2.getBottom());
like image 742
Balaji.K Avatar asked Jan 30 '11 05:01

Balaji.K


2 Answers

Easiest way is to get it using View.getLocationOnScreen(); OR getLocationInWindow();

And if you want position relative to root Layout then See

like image 93
Zar E Ahmer Avatar answered Sep 19 '22 22:09

Zar E Ahmer


Use the getLeft(), getRight(), getTop(), and getBottom(). These can be used after the view is instantiated.

like image 40
sanjay Avatar answered Sep 17 '22 22:09

sanjay