title: how to draw a circle on center of any device in android
body: <p>I want to draw a circle or any object on center of any device.but i am unable to get center of device in all device accurately.i am able to work on some device but on max its not drawing accurately.please help..thanks in advance.I am also posting my piece of code here as well</p>
  
  <pre><code> package com.app.maxcircle;
  
  import android.app.Activity;
  import android.content.Context;
  import android.content.res.Resources;
  import android.graphics.Bitmap;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.DashPathEffect;
  import android.graphics.Paint;
  import android.graphics.Paint.Style;
  import android.os.Bundle;
  import android.util.DisplayMetrics;
  import android.view.Display;
  import android.view.View;
  import android.view.ViewGroup.LayoutParams;
  import android.widget.LinearLayout;
  
  public class CircleWithMaxRadiusActivity extends Activity {
      /** Called when the activity is first created. */
  
      float pixelCenterX, pixelCenterY;
      DrawCanvasCircle pcc;
      LinearLayout ll;
      private Canvas canvas;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          Display display = getWindowManager().getDefaultDisplay();
          int width = display.getWidth();
          int height = display.getHeight();
          ll = (LinearLayout) findViewById(R.id.ll);
          float centerx = width / 2;
          float centery = height / 2;
          pixelCenterX = convertDpToPixel(centerx, this);
          pixelCenterY = convertDpToPixel(centery, this);
          System.out.println("px..." + pixelCenterX);
          System.out.println("py..." + pixelCenterY);
  
          pcc = new DrawCanvasCircle(this);
          Bitmap result = Bitmap.createBitmap(25, 25, Bitmap.Config.ARGB_8888);
          canvas = new Canvas(result);
          pcc.draw(canvas);
          pcc.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                  LayoutParams.FILL_PARENT));
          ll.addView(pcc);
      }
  
      public static float convertDpToPixel(float dp, Context context) {
          Resources resources = context.getResources();
          DisplayMetrics metrics = resources.getDisplayMetrics();
          float px = dp * (metrics.densityDpi / 160f);
          return px;
      }
  
      /**
       * This method converts device specific pixels to device independent pixels.
       * 
       * @param px
       *            A value in px (pixels) unit. Which we need to convert into db
       * @param context
       *            Context to get resources and device specific display metrics
       * @return A float value to represent db equivalent to px value
       */
      public static float convertPixelsToDp(float px, Context context) {
          Resources resources = context.getResources();
          DisplayMetrics metrics = resources.getDisplayMetrics();
          float dp = px / (metrics.densityDpi / 160f);
          return dp;
  
      }
  
      class DrawCanvasCircle extends View {
          public DrawCanvasCircle(Context mContext) {
              super(mContext);
          }
  
          protected void onDraw(Canvas canvas) {
              super.onDraw(canvas);
              Paint p = new Paint();
              p.setColor(Color.WHITE);
              DashPathEffect dashPath = new DashPathEffect(new float[] { 5, 5, 2,
                      2 }, (float) 1.0);
  
              p.setPathEffect(dashPath);
              p.setStyle(Style.STROKE);
  
              for (int i = 0; i &lt; 25; i++) {
                  canvas.drawCircle(pixelCenterX, pixelCenterY, pixelCenterY - 20
                          * i, p);
              }
  
              invalidate();
          }
      }
  
  }
  </code></pre>