Confusion in "smallest width dp" in Android

24k views Asked by At

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.

1

There are 1 answers

1
Diego On

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