title: How to convert dp to pixels and and use it to draw to canvas in android
body: <p>I'm trying to convert dp to pixels. I'm using this method:</p>
  
  <pre><code>//Converts device pixels to regular pixels to draw
  private float dpToPixel(float dp) {
      DisplayMetrics metrics = this.getResources().getDisplayMetrics();
      float px = dp * (metrics.densityDpi/160f);
      return px;
  }
  </code></pre>
  
  <p>Then I try to get the dp from a variable in my dimen.xml file, like this:</p>
  
  <pre><code>int buttonWidth = (int) dpToPixel(R.dimen.buttonHeight);
  </code></pre>
  
  <p>Then I create a scaled bitmap with the width of the <code>buttonWidth</code> variable.
  Then finally I draw it to the canvas. But when I try to run it, nothing is displayed. Can anyone help?</p>