Finding the View location (position) on display (screen) in android
Solution 1
Easiest way is to get it using
View.getLocationOnScreen();
OR getLocationInWindow();
And if you want position relative to root Layout then See
Solution 2
Use the getLeft()
, getRight()
, getTop()
, and getBottom()
. These can be used after the view is instantiated.
Solution 3
the methods u have written is enough to find the location on screen, but the place where you have written is not correct.
try to write the methods (view.getLeft(), view.getTop()... etc) in onWindowFocusChanged(boolean hasFocus) method.
for example...
**
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
System.out.println("Right:"+tv2.getRight());
System.out.println("Left:"+tv2.getLeft());
System.out.println("Top:"+tv2.getTop());
}
}
**
it will solve your issue.
Solution 4
You really can't get the view before hand. You may want to do an explicit using invalidate()
if possible or you can check this in the onPostResume()
or onResume()
function.
If you're just trying things out and want to have fun with threads, this code block will put your code onto the UI thread and will wait for everything to be rendered and then display your code:
final View gv = this;
ViewTreeObserver vto = gv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@SuppressLint("NewApi")
public void onGlobalLayout() {
gv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
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());
}
This last one is pretty heavy duty lifting but it will get the job done. I usually put any lines like this in my logs(i.e. android.util.Log
)
Solution 5
I'm not sure that you can get the position of the view before the layout phase have been done. Once the view is layed out you can use getLocationOnScreen
to get the position of the view. getTop
and similar only returns the position of the view in its parent.
Balaji.K
has been working as Android and iOS developer from 2012.
Updated on July 25, 2022Comments
-
Balaji.K almost 2 years
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 returned0
.
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());
-
ilkinulas over 12 yearsThese methods give the view position relative to its parent.
-
Royi Namir over 9 years@ilkinulas so what is the solution for the relative to the device ?
-
Mohsen Kamrani over 8 yearsThis is the perfect answer to this question.+1
-
Mallika Khullar almost 6 years
getLeft()
,getRight()
won't work every time as they return the position relative to the parent, not the screen.