title: How to get the absolute coordinates of a view
body: <p>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 <code>getLeft()</code> and <code>getRight()</code> don't work as they all seem to be relative to the parent of the view, thus giving me <code>0</code>. What is the proper way to do this?</p>
  
  <p>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 <code>getRawX()</code> and <code>getRawY()</code> from the <code>MotionEvent</code> 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 <code>getX()</code> and <code>getY()</code> on the <code>MotionEvent</code>, 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).</p>
  
  <p>Edit: This is the code I used to try to get the size of the holding container, as per one of the questions. <code>TableLayout</code> is the table which holds all the puzzle pieces.</p>
  
  <pre><code>TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
  Log.d(LOG_TAG, "Values " + tableLayout.getTop() + tableLayout.getLeft());
  </code></pre>
  
  <p>Edit 2: Here is the code I've tried, following more of the suggested answers.</p>
  
  <pre><code>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]);
  </code></pre>
  
  <p>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 <code>TableLayout</code>. Cells[0] is the top left <code>ImageView</code>, and I picked cells[4] as it's somewhere in the middle and most definitely should not have coordinates of (0,0).</p>
  
  <p>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.)</p>
  
  <p>I don't know if this is significant, but the <code>ImageView</code>s are originally not given a size. The size of the <code>ImageView</code>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 <code>tableLayout.requestLayout()</code> as shown above, but that didn't help.</p>