3

I am learning how to use "smallest width dp" to support different screens using this in android.

i get that below number are the smallest of side of device in dp.

Typical numbers for screen width dp are:

320: a phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).

480: a tweener tablet like the Streak (480x800 mdpi).

600: a 7” tablet (600x1024).

720: a 10” tablet (720x1280, 800x1280, etc).

According to this post nexus 6 has 730 x 410 dp.

The Nexus 6 boasts an impressive 5.96” Quad HD screen display at a resolution of 2560 x 1440 (493 ppi). This translates to ~ 730 x 410 dp (density independent pixels).

but as explained in this

           dp = (px/dpi)*160
              = (1440/493)*160
              = ~467

then how come this translates to 730 x 410 dp? further more when i run the demo in nexus 6, device is using padding dimension defined under res/values-sw320dp/dimens.xml

this confuses me. how does one actually calculates dp and create view accordingly using "smallest width dp" ?

apart from res/values-sw320dp i have res/value and res/values-sw600dp that has dimens.xml under it.

UPDATE figured out my confusion. check comment under question.

Ankit
  • 270
  • 1
  • 4
  • 12
  • 1
    check [this](http://stackoverflow.com/questions/16105032/confusion-with-smallest-width-600-dp-selector)... it may help u – Biplab Jun 16 '15 at 13:50
  • I figured it out using this. [link](http://stackoverflow.com/questions/30041594/how-does-quantized-density-affect-image-resource-selection-and-scaling). dp is calculated by quantized density not physical density. so 730 x 410 dp for nexus 6 is correct. – Ankit Jul 23 '15 at 05:14
  • 1
    And sw320dp part confusion was my mistake in understanding. res/values-sw320dp is used because next bucket defined is res/values-sw600dp which obviously is larger that 410(min side if nexus 6). – Ankit Jul 23 '15 at 05:22

1 Answers1

5

You can get the correct values with the following code:

DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
    float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
    float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
    Log.d("dpHeight-----",String.valueOf(dpHeight));
    Log.d("dpWidth------",String.valueOf(dpWidth));

For example for the Xperia Z 1080x1920px the values are 360x592dp, the settings is values-sw360dp

Diego
  • 699
  • 6
  • 10
  • yes, this gives right values. DisplayMetrics.density is used for scaling resources which depends on quantized density. – Ankit Jul 23 '15 at 05:41