Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 920
LittleFunny Avatar asked Oct 01 '13 10:10

LittleFunny


People also ask

How do you calculate dp in pixels?

Device-independent pixels (dp) For this mapping, 1 dp is considered to be equal to 1 pixel on a 160 dpi resolution screen. The corresponding number of pixels can be calculated with the formula px = dp * (dpi/160).

How is pixel density calculated?

You can take either the horizontal or vertical resolution. Once you have the resolution, you must measure the length of the display. Usually, this will be measured in inches (2.54cm = 1 inch). If you divide the two numbers, you'll find the “pixels per inch” (ppi).

How do I find my device DPI?

Finding Your DPI There are a handful of apps that can show you your DPI value, but the clear-cut best option is DPI Checker by TheViciousGames. It's super lightweight, open source, and does exactly what you need it to do and nothing more.

Is dp equal to pixel?

To preserve the visible size of your UI on screens with different densities, you must design your UI using density-independent pixels (dp) as your unit of measurement. One dp is a virtual pixel unit that's roughly equal to one pixel on a medium-density screen (160dpi; the "baseline" density).


2 Answers

Nobody answered the question: S4: Resolution 1080 x 1920 pixels (~441 ppi pixel density) per gsmarena formula: px = dp * (dpi / 160) from Google's article on "Supporting multiple screens". Therefore: dp = px / (dpi / 160). So, shortest width (sw) = 1080/(441/160) = 391

As also stated here: Use size and density-specific resources problems in Android app design

like image 160
codeslapper Avatar answered Sep 27 '22 17:09

codeslapper


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;

like image 34
AlexAndro Avatar answered Sep 27 '22 17:09

AlexAndro