1

I have a smartphone of 2560 x 1440 px. Now I am using this function for my TextView:

int[] locationOnScreen = new int[2];
txtAp.GetLocationInWindow(locationOnScreen);

It is supposed to give me the total x and y coordinates in pixels. My TextView is pretty much in the middle of the screen, so supposedly at (1280,770). But the function returns [69, 1111].

How can that be? If that is not the way of doing that, then what is?

Onik
  • 19,396
  • 14
  • 68
  • 91
innomotion media
  • 862
  • 11
  • 30

1 Answers1

1

I have a smartphone of 2560 x 1440 px... My TextView is pretty much in the middle of the screen, so supposedly at (1280,770).

Not correct, unless you talk about the center of the TextView. Any view is rendered on screen within its rectangle, so its coordinates are considered to be of [left, top, right, bottom].

The GetLocationInWindow() method returns (left, top) coordinates of a view. So (x = 69, y = 1111) looks to be meaningful for the left top corner of your TextView located in the middle of screen.

Important note: GetLocationInWindow() returns the coordinates w.r.t the root view, not actual window. You should take the status bar height into consideration. And here is how you can calculate Height of status bar in Android.

Onik
  • 19,396
  • 14
  • 68
  • 91