I'm trying to get the absolute screen pixel coordinates of the top left corner of a view. However, all methods I can find such as getLeft()
and getRight()
don't work as they all seem to be relative to the parent of the view, thus giving me 0
. What is the proper way to do this?
If it helps, this is for a 'put the picture back in order' game. I want the user to be able to draw a box to select multiple pieces. My assumption is that the easiest way to do that is to getRawX()
and getRawY()
from the MotionEvent
and then compare those values against the top left corner of the layout holding the pieces. Knowing the size of the pieces, I can then determine how many pieces have been selected. I realise I can use getX()
and getY()
on the MotionEvent
, but as that returns a relative position that makes determining which pieces were selected more difficult. (Not impossible, I know, but it seems unnecessarily complicated).
Edit: This is the code I used to try to get the size of the holding container, as per one of the questions. TableLayout
is the table which holds all the puzzle pieces.
TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
Log.d(LOG_TAG, "Values " + tableLayout.getTop() + tableLayout.getLeft());
Edit 2: Here is the code I've tried, following more of the suggested answers.
public int[] tableLayoutCorners = new int[2];
(...)
TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
tableLayout.requestLayout();
Rect corners = new Rect();
tableLayout.getLocalVisibleRect(corners);
Log.d(LOG_TAG, "Top left " + corners.top + ", " + corners.left + ", " + corners.right
+ ", " + corners.bottom);
cells[4].getLocationOnScreen(tableLayoutCorners);
Log.d(LOG_TAG, "Values " + tableLayoutCorners[0] + ", " + tableLayoutCorners[1]);
This code was added after all the initialisation is done. The image has been divided up into a array of ImageViews (the cells[] array) contained within a TableLayout
. Cells[0] is the top left ImageView
, and I picked cells[4] as it's somewhere in the middle and most definitely should not have coordinates of (0,0).
The code shown above still gives me all 0s in the logs, which I really don't understand because the various puzzle pieces are correctly displayed. (I tried public int for tableLayoutCorners and default visibility, both giving the same result.)
I don't know if this is significant, but the ImageView
s are originally not given a size. The size of the ImageView
s is determined during the initialisation automatically by the View when I give it an image to display. Could this contribute to their values being 0, even though that logging code is after they have been given an image and have automatically resized themselves? To potentially counter that, I added the code tableLayout.requestLayout()
as shown above, but that didn't help.
Use This code to find exact X and Y cordinates :
I have added/subtracted some values to adjust my view. Please do these lines only after whole view has been inflated.
The accepted answer didn't actually tell how to get the location, so here is a little more detail. You pass in an
int
array of length 2 and the values are replaced with the view's (x, y) coordinates (of the top, left corner).Notes
getLocationOnScreen
withgetLocationInWindow
should give the same results in most cases (see this answer). However, if you are in a smaller window like a Dialog or custom keyboard, then use you will need to choose which one better suits your needs.(0,0)
if you call this method inonCreate
because the view has not been laid out yet. You can use aViewTreeObserver
to listen for when the layout is done and you can get the measured coordinates. (See this answer.)Following Romain Guy's comment, here's how I fixed it. Hopefully it'll help anyone else who also had this problem.
I was indeed trying to get the positions of the views before they had been laid out on the screen but it wasn't at all obvious that was happening. Those lines had been placed after the initilisation code ran, so I assumed everything was ready. However, this code was still in onCreate(); by experimenting with Thread.sleep() I discovered that the layout is not actually finalised until after onCreate() all the way to onResume() had finished executing. So indeed, the code was trying to run before the layout had finished being positioned on the screen. By adding the code to an OnClickListener (or some other Listener) the correct values were obtained because it could only be fired after the layout had finished.
The line below was suggested as a community edit:
please use
onWindowfocuschanged(boolean hasFocus)
Use
View.getLocationOnScreen()
and/orgetLocationInWindow()
.My utils function for get view location, it will return a
Point
object withx value
andy value
Using
First you have to get the localVisible rectangle of the view
For eg:
Hope this will help