Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect Coordinates From getLocationOnScreen/getLocationInWindow

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.

like image 251
nimph Avatar asked Apr 14 '10 14:04

nimph


1 Answers

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; 
like image 86
nimph Avatar answered Oct 19 '22 12:10

nimph