Search for the location (position) of viewing on the display (screen) in android

I want to find the presentation position on the display screen.

For this, I use methods such as view.getLeft() , view.getBottom() , view.getRight() , view.getTop() . But unfortunately all of these methods return 0 .
Is there an alternative way to find the position of the view (for example, the coordinates on the screen)?

My main.xml layout:

 <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()); 
+8
android android-layout android-view
source share
5 answers

Use getLeft() , getRight() , getTop() and getBottom() . They can be used after creating the view.

+7
source share

The easiest way is to use it View.getLocationOnScreen(); OR getLocationInWindow();

And if you want a position relative to the root Layou t, then see

+6
source share

enough written methods u find the place on the screen, but the place where you wrote is incorrect.

try writing methods (view.getLeft (), view.getTop () ... etc.) in the onWindowFocusChanged (boolean hasFocus) method.

eg...

**

 @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()); } } 

**

he will solve your problem.

+5
source share

You really cannot get the idea before hand. You can explicitly use invalidate() if possible, or you can check this in the onPostResume() or onResume() function.

If you're just trying to figure it out and want to enjoy the threads, this block of code will put your code in the UI thread and wait for everything to be displayed, and then your code will display:

 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 is quite a difficult climb, but he will do his job. I usually put such lines in my logs (i.e. android.util.Log )

+3
source share

I'm not sure if you can get a view position before the layout phase is completed. After the view has been laid out, you can use getLocationOnScreen to get the position of the view. getTop and the like returns only the position of the view in the parent.

+1
source share

All Articles