title: Get the coordinates of the center of a view
body: <p>I am trying to get the center coordinates of my custom view (a circle) so that I can draw a line from the those points.  The custom view is inside a <code>TableLayout</code> which is itself inside a <code>FrameLayout</code>. However, I am having trouble - this code doesn't get it right. Any advice please?</p>
  
  <p>The values given by this method are wrong on all devices. If I change the layout padding/margins etc. then the dots obviously move, but the point given by this method does not change.</p>
  
  <pre><code>public float[] getDotCenterLocationOnScreen() {
  
          int[] location = new int[2];
          getLocationOnScreen(location);
  
          int xLoc = location[0];
          int yLoc = location[1];
  
          float xCenter = xLoc + (getWidth()/2);
          float yCenter = yLoc - (getWidth()/2);
  
          float[] dotCenterLocation =  { xCenter, yCenter };
  
          return dotCenterLocation;
  
      }
  </code></pre>
  
  <p>Here is most of the code from my view class:</p>
  
  <pre><code>        // Radius of Dot
          int RADIUS;
          private static final int START_RADIUS = 30;
  
          // Value of which the Radius is multiplied by to get full width &amp; height of
          // the DotView
          int sizeMultiplier = 4;
  
          // Define other Objects
          private Paint paint = new Paint();      
  
          float mTranslateX;
          float mTranslateY;
  
          public DotView(Context context, AttributeSet attrs) {
              super(context, attrs);
  
              paint.setAntiAlias(true);
              paint.setStrokeWidth(6f);
              paint.setStyle(Paint.Style.FILL);
              paint.setStrokeJoin(Paint.Join.ROUND);
              paint.setColor(Color.BLACK);
  
              RADIUS = START_RADIUS;
  
          }
  
          public void setRadius(int radius) {
              RADIUS = radius;
              invalidate();
          }
  
          public int getRadius() {
              return RADIUS;
          }
  
          public void onDraw(Canvas canvas) {
  
              super.onDraw(canvas);
              canvas.save();
              canvas.translate(mTranslateX, mTranslateY);
              canvas.drawCircle(0, 0, RADIUS, paint);
              canvas.restore();
          }
  
          public float[] getDotCenterLocationOnScreen() {
  
              int[] location = new int[2];
              getLocationOnScreen(location);
  
              int xLoc = location[0];
              int yLoc = location[1];
  
              float xCenter = xLoc + (getWidth()/2);
              float yCenter = yLoc - (getWidth()/2);
  
              float[] dotCenterLocation =  { xCenter, yCenter };
  
              return dotCenterLocation;
  
          }
  
          @Override
          protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
              super.onMeasure(widthMeasureSpec, heightMeasureSpec);
              final int dia = START_RADIUS * sizeMultiplier; // Multiplying by 2 makes
                                                              // boundaries exactly the same size a dot.
  
              int w = resolveSize(dia, widthMeasureSpec);
              int h = resolveSize(dia, heightMeasureSpec);
              setMeasuredDimension(w, h);
              float radius = Math.min(w, h) / 2F;
              mTranslateX = radius;
              mTranslateY = radius;
  }
  
      }
  </code></pre>