Question

I was reading dp, dip, px, sp measurements, but I still have some questions about dp/dpi vs ppi vs px vs inch. I am not able to compare them... is an inch the largest?

They say 160 dpi means 160 pixels per one inch. Does that mean 1 inch contains 160 pixels?

They also say 1 pixel on a 160 dpi screen = 1 dp. Does that mean 1 pixel and 1 dp are equal?

And lastly, why should we use dp instead of px? I understand that it is ideal, but why?

Was it helpful?

Solution

You should (almost) always use flexible sizing units, like dp, which is Density-Independent Pixels, because 300px on one device is not necessarily the same amount of screen real estate as 300px on another. The biggest practical implication is that your layout would look significantly different on devices with a different density than the one your design targeted.

  • dp or dip means Density-independent Pixels
  • dpi or ppi means Dots (or Pixels) Per Inch
  • inch is a physical measurement connected to actual screen size
  • px means Pixels — a pixel fills an arbitrary amount of screen area depending on density.

For example, on a 160dpi screen, 1dp == 1px == 1/160in, but on a 240dpi screen, 1dp == 1.5px. So no, 1dp != 1px. There is exactly one case when 1dp == 1px, and that's on a 160dpi screen. Physical measurement units like inches should never be part of your design—that is, unless you're making a ruler.

A simple formula for determining how many pixels 1dp works out to is px = dp * (dpi / 160).

OTHER TIPS

dp is a physical measurement like inches. (Yes, it is. Read on.)

"A dp corresponds to the physical size of a pixel at 160 dpi" (https://developer.android.com/training/multiscreen/screendensities.html#TaskUseD)

The physical size of a pixel at 160 dpi is exactly 1/160th of an inch. Therefore the size of a dp is 1/160th of an inch. 160 dp = 1 inch.

Px is a somewhat arbitrary unit of measurement on a screen.

For examples of what dp converts to in px on different devices, see here:

https://stackoverflow.com/a/39495538/984003

How do dp, dip, dpi, ppi, pixels and inches relate?

For the purpose of android development:

dp = dip
dpi = ppi
inch x dpi = pixels
dp = 160 x inch
dp = 160*pixels/dpi

So, on a 160dpi phone (mdpi):

2 inches = 320 dp
2 inches = 320 pixels

On a 180 dpi phone:

2 inches = 320 dp
2 inches = 360 pixels

Note that 2 inches is ALWAYS 320dp, independent of screen size. A dp is a physical distance of 1/160th of an inch.

The dp to pixels formula is interesting:

dp = 160*pixels/dpi

Is equivalent to:

dp = pixels/(dpi/160)

dpi/160 is an interesting factor. Its the relative density compared to android's mdpi bin and the amount you must scale your graphics by for the various resource bins. You'll see that factor mentioned a few times on this page, 0.75 being the factor to ldpi.

I will explain using an example.

float density = context.getResources().getDisplayMetrics().density;
float px = someDpValue * density;
float dp = somePxValue / density;

density equals

.75 on ldpi (120 dpi)
1.0 on mdpi (160 dpi; baseline)
1.5 on hdpi (240 dpi)
2.0 on xhdpi (320 dpi)
3.0 on xxhdpi (480 dpi)
4.0 on xxxhdpi (640 dpi)

so for example,

I have a Samsung S5 with 432 dpi (http://dpi.lv/#1920×1080@5.1″).

So, density = 432/160 = phone's dpi/baseline = 2.7

Let say my top bar is 48dp. This is referenced to baseline (160dpi).

So, w.r.t my S5, it will be 48dp * 2.7.

Then if I want to see the actual height:

It will be (48dp * 2.7) / 432 dpi = 0.3 inches.

DP is the resolution when you only factor the physical size of the screen. When you use DP it will scale your layout to other similar sized screens with different pixel densities.

Occasionally you actually want pixels though, and when you deal with dimensions in code you are always dealing with real pixels, unless you convert them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top