4

I'm sorry for posting this again, I posted a nearly identical question but maybe it's too long and I got no answer. I'll get in the point directly for this question
So I made a floating icon like Facebook's ChatHead but I have some problems with its X and Y coordinates. I need to get the phone screen's for Top, Bottom, Left and Right edges to solve the problems.
What I don't get is that what are these X and Y based on? My Note 2 screen is 1280x720 but when I move the head to the Top and Bottom edge I got these random numbers from Log:

  12-28 11:41:49.471: D/COORDINATES X Y(9879): 73 -571 (Top)
  12-28 11:42:05.991: D/COORDINATES X Y(9879): 23 572 (Bottom)

(There is a deviation with the value due to my code's precision which is the problem I mentioned above but it's only in the range of 1-5)
I tried to sum 2 absolute Y values but the result's nowhere near 1280 which is my screen resolution so right now I'm very confuse about this?
Hope someone can help me in this, I've spent hours searching for this but still nothing comes up
FOR MORE DETAIL: This is the code of the head: (with x=0 and y=0 it'll appear in the center of the screen)

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        params.x = 0;
        params.y = 0;


        windowManager.addView(chatHead, params);

EDIT: @Jason said I should use screen's dimension (width and height of the screen if my understanding is right). But I need X and Y for moving the head and they're not based on screen's dimension. This is the link to the full code in case anyone needs: http://www.piwai.info/chatheads-basics/

Smurfy
  • 65
  • 1
  • 7
  • Docs say they're offsets from the given edge, as you've probably read. I've re-read you're question a few times, but I'm still not sure what you're trying to accomplish. If you need screen dimensions for some calculation, why need get them through alternate means such as this old chestnut: http://stackoverflow.com/questions/1016896/get-screen-dimensions-in-pixels – JASON G PETERSON Dec 28 '14 at 06:03
  • Yeah I've read about it and the reason why I want alternate means is that I need the X and Y for onTouchListener to move the Head. The code is from a Service so I can't use Display's getWindowManager() and get screen's width and height form it(Sorry if I get your idea wrong, I'm not a native English speaker and my English isn't very good) – Smurfy Dec 28 '14 at 06:18
  • One more thing: what I want to know is that they're offsets from the given edge but in what percentage?(Again, sorry if I understand the word offset wrong) – Smurfy Dec 28 '14 at 06:22
  • They're just pixels, as far as I know. – JASON G PETERSON Dec 28 '14 at 06:28
  • Yeah and I thought just like you but the absolute value of top Y and bottom Y when summed up is only about 1140 which is less than 1280(my screen's 1280x720). That's what makes me confused. – Smurfy Dec 28 '14 at 06:34
  • But you may need to subtract out the height of the Actionbar. (Just a guess.) – JASON G PETERSON Dec 28 '14 at 06:37
  • Good point, I didn't notice the action bar. But these are left edge X and right edge X:. -318 (Left). 324 (Right). which when summed up is about 640 (Like I mentioned in the question there's a deviation but it's very small). Besides, I don't think the action bar take that much ~140 units. – Smurfy Dec 28 '14 at 06:43

2 Answers2

2

I am having a similar problem with you. and I tested my app WindowManager.LayoutParam.x and WindowManager.LayoutParam.y. (My phone is Galaxy 4S and maximum size is 1920x1280)

I figured it out the answer.

the based value is the very relative value to the 'first location value of ' the View when it is first loaded.

2

Set both gravity as well as x and y co-ordinates. Example:

params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 860;
params.y = 0;

This would move the view to around top center.. depending on your screen size.

Yaseen Ahmad
  • 1,807
  • 5
  • 25
  • 43