Search code examples
androidresolutiondensity-independent-pixel

How to calculate the pixel density (dp) of a device?


What is the independent pixel density for a Galaxy s4?

I need it so I can have a qualifier sw???dp for that phone.

It would be great if you could explain how to calculate it.


Solution

  • Here you can find how to calculate sw???dp for every device you want.

    Application Skeleton to support multiple screen

    I think that instead of 96 x 5' (screen size) you should use these qualifiers: http://developer.android.com/design/style/devices-displays.html (160, 240, 320, 480). In this case of Galaxy S4, you get the good result, but if you try for example with a Nexus4 (4,7 ') you will get sw~272 instead of 380 as it should, and as happens in practice. This is the value you should use in your formula and you will get the right result.

    OR

    run this snippet code: (API 13+)

    int sw = getResources().getConfiguration().smallestScreenWidthDp;