How to Get the Absolute Coordinates of a View

How to get the absolute coordinates of a view

Use View.getLocationOnScreen() and/or getLocationInWindow().

Get absolute coordinates of view inside popup window

Use View#getLocationOnScreen(int[]) API:

Computes the coordinates of this view on the screen. The argument must be an array of two integers. After the method returns, the array contains the x and y location in that order.



int[] location = new int[2];
getView().getLocationOnScreen(location);
// now location[0] contains x position
// and location[1] contains y position

How to get the x & Y coordinates of a view

To ensure view has had a chance to update, run your location request after the View's new layout has been calculated by using view.post:

view.post {
val point = IntArray(2)
view.getLocationOnScreen(point)
val (x, y) = point
}

Values SHOULD no longer be 0 but i had bad experiences with huawei devices. Other way to get x and y coordinates is using viewTreeObserver

view.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
view.viewTreeObserver.removeOnGlobalLayoutListener(this)
val point = IntArray(2)
view.getLocationOnScreen(point)
val (x, y) = point
}
});

Possibly you experience some visual glitch with this approach

How to get absolute coordinates from Views in a RelativeLayout

Here is the Solution:

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int statusbarHeight = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight()
- YOURVIEW.getHeight();
int[] position = new int[2];
vline.getLocationInWindow(position);
scrollView.smoothScrollBy(0, (position[1]-statusbarHeight));
}
});

Android get view location on screen

It might be because you tried to get position before onLayout has happened.

You might try something like this:

ViewTreeObserver vto=view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
@Override public void onGlobalLayout(){
int [] location = new int[2];
view.getLocationOnScreen(location);
x = location[0];
y = location[1];
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}

Also check how to get view's position in coordinates?

how to get view's position in coordinates?

View Tree Observer callback will be called after the view has been rendered on screen. Above methods will always yield 0 as view/layout has not been rendered yet.

ViewTreeObserver vto=view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
@Override public void onGlobalLayout(){
int [] location = new int[2];
view.getLocationOnScreen(location);
x = location[0];
y = location[1];
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);

}
}

Getting View's coordinates relative to the root layout

This is one solution, though since APIs change over time and there may be other ways of doing it, make sure to check the other answers. One claims to be faster, and another claims to be easier.

private int getRelativeLeft(View myView) {
if (myView.getParent() == myView.getRootView())
return myView.getLeft();
else
return myView.getLeft() + getRelativeLeft((View) myView.getParent());
}

private int getRelativeTop(View myView) {
if (myView.getParent() == myView.getRootView())
return myView.getTop();
else
return myView.getTop() + getRelativeTop((View) myView.getParent());
}

Let me know if that works.

It should recursively just add the top and left positions from each parent container.
You could also implement it with a Point if you wanted.

How to get the absolut position of a touch event?

You can use event.getRawX() and event.getRawY() to get position relative to screen.



Related Topics



Leave a reply



Submit