title: Android: Screen Density is Not Being Reported Correctly
body: <p>In the course of developing my Android application, I noticed that the following lines of code were not reporting the correct screen size for my Samsung Nexus S:</p>
  
  <pre><code>width = getResources().getDisplayMetrics().widthPixels;
  height = getResources().getDisplayMetrics().heightPixels;
  </code></pre>
  
  <p>Width was returned as 320, while height got set to 533. From <a href="https://stackoverflow.com/questions/5078808/android-showing-wrong-screen-resolution">this post</a> I know that this is a problem relating to the pixel density of the device not being taken into account. To correct for this, I wrote up the code below:</p>
  
  <pre><code>DisplayMetrics dm = new DisplayMetrics(); 
  getWindowManager().getDefaultDisplay().getMetrics(dm);
  
  int density = dm.densityDpi; 
  int width = (density / 160) * dm.widthPixels; 
  int height = (density / 160) * dm.heightPixels;
  </code></pre>
  
  <p>When I toast the variables <code>width</code> and <code>height</code>, the returned dimensions are 320 wide and 533 tall, meaning that nothing changed. Furthermore, <code>dm.densityDpi</code> returns 160, and not 235, as it should for the Samsung Nexus S.</p>
  
  <p>Some posts that I have read, seem to suggest that the problem might lie within the AndroidManifest.xml. I have already added the following to my XML file in an attempt to solve this issue:</p>
  
  <pre><code>&lt;uses-sdk 
      android:minSdkVersion="7" 
      android:targetSdkVersion="10"
  /&gt;
  
  &lt;supports-screens 
      android:anyDensity="false"
      android:resizeable="false"
      android:largeScreens="true"
      android:normalScreens="true"
  /&gt;
  </code></pre>
  
  <p>But, alas, this has not worked either. So, does anyone know how I can get the phone to correctly return its density?</p>