63

A call to getLocationOnScreen() or getLocationInWindow() both give me a top/Y coordinate that is about ~30px (status/notifications bar's height) too far down. The left/X coordinate is dead on.

As I hinted above, I believe the difference is because of the status/notification bar... I could be wrong. I think I can solve this if I can determine the size of the notification bar but, I'm having trouble doing just that.

Any help would be greatly appreciated.

nimph
  • 1,597
  • 1
  • 10
  • 11

7 Answers7

86

I ended up solving this issue by determining the height of the status/notification bar like so:

View globalView = ...; // the main view of my activity/application

DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
int topOffset = dm.heightPixels - globalView.getMeasuredHeight();

View tempView = ...; // the view you'd like to locate
int[] loc = new int[2]; 
tempView.getLocationOnScreen(loc);

final int y = loc[1] - topOffset;
ForceMagic
  • 6,230
  • 12
  • 66
  • 88
nimph
  • 1,597
  • 1
  • 10
  • 11
  • This worked in normal configuration, but if the activity adjusts 'globalView' height when SoftInput/Keyboard is opened, then keyboard height is also counted to 'topOffset', this results in wrong value for 'y'. – Thamme Gowda Jan 06 '14 at 15:09
  • 8
    `int [] locInWindow = new int [2]; globalView.getLocationInWindow(locInWindow); topOffset = locInWindow[1];` //Compute `topOffset` by getting Y coordinate of `globalView` – Thamme Gowda Jan 06 '14 at 15:39
  • @ThammeGowda could please explain more how i can get the top offset when the keyboard is openes – Hussien Fahmy Oct 17 '21 at 14:07
10

Here is how I like to get the status bar height, and adjust the offset:

final int[] location = new int[2];
anchor.getLocationInWindow(location); // Includes offset from status bar, *dumb*
Rect anchorRect = new Rect(location[0], location[1],
        location[0] + anchor.getWidth(), location[1] + anchor.getHeight());

anchor.getRootView().findViewById(android.R.id.content).getLocationInWindow(location);
int windowTopOffset = location[1];
anchorRect.offset(0, -windowTopOffset);
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
satur9nine
  • 13,927
  • 5
  • 80
  • 123
4

This answer does not include how to get the status bar height, but it does explain the behavior of getLocationOnScreen() and getLocationInWindow() returning the same value.

In the case of a normal activity (not a dialog) you should expect these two methods to return the same value. A Window lays out underneath (as in z-order not y coordinates) the status bar, so these methods cannot be used to determine the height of the status bar.

https://stackoverflow.com/a/20154562/777165

Community
  • 1
  • 1
groucho
  • 1,835
  • 1
  • 13
  • 15
  • in Dialog view, it works fine if not set the window full screen is true, but if set as follows: true then the value is not correct, so need subtract the 'topOffset' – iXcoder Mar 31 '15 at 02:13
4

I am having the same problem, try using

offset = myView.GetOffsetY();

and adjust your Y coord by that value, e.g.

coordY -= offset;

The class which offers the ``-method:

class MyView extends View {

  public int GetOffsetY() {
    int mOffset[] = new int[2];
    getLocationOnScreen( mOffset );
    return mOffset[1]; 
  }

}
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
user330844
  • 872
  • 1
  • 12
  • 12
  • I don't see "getOffsetY()". What class is that a method of? – nimph May 03 '10 at 20:55
  • I am sorry, I failed to post the method, in class MyView extends View { public int GetOffsetY() { int mOffset[] = new int[2]; getLocationOnScreen( mOffset ); return mOffset[1]; } – user330844 Jul 13 '10 at 14:17
1

As @ThammeGowda said that the correct answer will give incorrect position when the keyboard is shown but you still need to calculate the top offset of the action bar, so just get the action bar size directly

fun View.getPosition(): Point {
    val tv = TypedValue()
    if (context.theme.resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
        val actionBarHeight =
            TypedValue.complexToDimensionPixelSize(tv.data, resources.displayMetrics)

        val loc = IntArray(2).apply { getLocationInWindow(this) }

        val newY = loc[1] - actionBarHeight - (measuredHeight / 2) // to get the position in the middle of the view

        return Point(loc[0], newY)
    }
    return Point(0,0)
}

enter image description here

Hussien Fahmy
  • 1,119
  • 6
  • 24
0
 void setOffset(){
  int[] point1=new int[2];
  view1.getLocationInWindow(point1);
  
  int[] point2=new int[2];
  view2.getLocationInWindow(point2);

  int leftOffset=point2[0]-point1[0];
  int topOffset=point2[1]-point1[1];  
  // no need to call this function more than once
  // set the offset values in global variables 
  // remenber do't call it in OnCreate method
  // this helps to mesure all type of screen size
}

void setLocationOfView(){
  // now do your animation or any view to move
 int[] point =new int[2];
 sourceView.getLocationInWindow(point);
 view.setX(point[0]-leftOffset);
 view.setY(point[1]-topOffset);
 //wow you did it i am glad to help you and sorry for being late
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 27 '22 at 00:29
0

Solution:

  1. Put an invisible png ImageView at the top with
android:layout_marginTop="0dp"
  1. Get location on screen of the invisible png. This will be your offset for the y-coordinate bug.
int[] location = new int[2];

invisiblepng.getLocationOnScreen(location);

int sourceY = location[1];

SharedPreferences.Editor editor = sp.edit();

editor.putString("compensateybug", Integer.toString(sourceY));

editor.apply();
 and
public static Point getLocationOnScreen(View view) {
int[] location = new int[2];
view.getLocationOnScreen(location);
return new Point(location[0], location[1]);
}
  1. Retrieve Y coordinate value of the "status/notification bar"
String compensateybug = sp.getString("compensateybug", "0");

int yourycoordinate = parseInt(compensateybug);

I have attached an invisible png image file for download at:

https://drive.google.com/file/d/1zxhP1r6ISvKG8Ig8iItDY2x6k5RVo4Nu/view?usp=sharing

Kevin
  • 109
  • 6