Relation Between Dp - Sp and Px

What is the difference between px, dip, dp, and sp?

From the Android Developer Documentation:


  1. px

    Pixels - corresponds to actual pixels on the screen.


  2. in

    Inches - based on the physical size of the screen.

    1 Inch OR 2.54 centimeters


  3. mm

    > Millimeters - based on the physical size of the screen.


  4. pt

    > Points - 1/72 of an inch based on the physical size of the screen.


  5. dp or dip

    > Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160
    dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".


  6. sp

    > Scaleable Pixels OR scale-independent pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommended you
    use this unit when specifying font sizes, so they will be adjusted
    for both the screen density and the user's preference. Note, the Android documentation is inconsistent on what sp actually stands for, one doc says "scale-independent pixels", the other says "scaleable pixels".

From Understanding Density Independence In Android:

















































Density BucketScreen DensityPhysical SizePixel Size
ldpi120 dpi0.5 x 0.5 in0.5 in * 120 dpi = 60x60 px
mdpi160 dpi0.5 x 0.5 in0.5 in * 160 dpi = 80x80 px
hdpi240 dpi0.5 x 0.5 in0.5 in * 240 dpi = 120x120 px
xhdpi320 dpi0.5 x 0.5 in0.5 in * 320 dpi = 160x160 px
xxhdpi480 dpi0.5 x 0.5 in0.5 in * 480 dpi = 240x240 px
xxxhdpi640 dpi0.5 x 0.5 in0.5 in * 640 dpi = 320x320 px

Difference between dp and sp (The other definitions that I have seen was still confusing me)?

DP (density-independent pixels) is 1 pixel on a 160 dpi screen.

SP (Scale-independent Pixels) - this resembles the dp unit, however it is likewise scaled by the user's text dimension setting.

Relation between dp - sp and PX

I recommend reading Google's definitions of dp and sp, which can be found in the Android docs, here and here.

There's also some helpful information in the wonderful Designer's Guide to DPI.

When should I use sp, dp, px, in and mm? (Android)

Use dp (dip, device independent pixels). These are like pixels, except they treat every device, regardless of its actual pixel density, as if it had a pixel density of, I think 160dpi.

When you are specifying the size of text, use sp. It is to points (the standard way of specifying font size) what dp is to pixels.

Do not ever, ever use px (pixels). Same for pt (points).

If you are writing an application that is a ruler, you have a good reason for using mm and in. Not otherwise.

Better yet, use match_parent and wrap_content!

What is the difference between px, dip, dp, and sp?

From the Android Developer Documentation:


  1. px

    Pixels - corresponds to actual pixels on the screen.


  2. in

    Inches - based on the physical size of the screen.

    1 Inch OR 2.54 centimeters


  3. mm

    > Millimeters - based on the physical size of the screen.


  4. pt

    > Points - 1/72 of an inch based on the physical size of the screen.


  5. dp or dip

    > Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160
    dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".


  6. sp

    > Scaleable Pixels OR scale-independent pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommended you
    use this unit when specifying font sizes, so they will be adjusted
    for both the screen density and the user's preference. Note, the Android documentation is inconsistent on what sp actually stands for, one doc says "scale-independent pixels", the other says "scaleable pixels".

From Understanding Density Independence In Android:

















































Density BucketScreen DensityPhysical SizePixel Size
ldpi120 dpi0.5 x 0.5 in0.5 in * 120 dpi = 60x60 px
mdpi160 dpi0.5 x 0.5 in0.5 in * 160 dpi = 80x80 px
hdpi240 dpi0.5 x 0.5 in0.5 in * 240 dpi = 120x120 px
xhdpi320 dpi0.5 x 0.5 in0.5 in * 320 dpi = 160x160 px
xxhdpi480 dpi0.5 x 0.5 in0.5 in * 480 dpi = 240x240 px
xxxhdpi640 dpi0.5 x 0.5 in0.5 in * 640 dpi = 320x320 px

What should I use for margins, sp or dp?

sp for font sizes, dp for everything else. sp stands for Scale-independent Pixels, dp stands for dip=density independent pixels. Detailed explanation

Which measurement we should use in android, px, dp, dip or sp?

dip is best used for when you need something a static size. This is a "Device Independant Pixel" and will roughly be rendered the same physical size on any handset/tablet.

Ideally though, you should be using layout_weights and clever implementations of match_parent and wrap_content to fill most fields.

Edit: Just FYI, dip = dp ... compiler handles them equivocally. Use whichever you like.

Relations between dip, px and dpi

That question is fully covered by official documentation. Relations between dip, px and dpi are covered by this section.

Quote:

Density-independent pixel (dp)

A virtual pixel unit that applications can use in defining their
UI, to express layout dimensions or
position in a density-independent way.

The density-independent pixel is equivalent to one physical pixel on a
160 dpi screen, the baseline density
assumed by the platform (as described
later in this document). At run time,
the platform transparently handles any
scaling of the dp units needed, based
on the actual density of the screen in
use. The conversion of dp units to
screen pixels is simple: pixels = dps
* (density / 160). For example, on 240 dpi screen, 1 dp would equal 1.5
physical pixels. Using dp units to
define your application's UI is highly
recommended, as a way of ensuring
proper display of your UI on different
screens.

So the statement:

that always be 12px in mdpi and so 18px in hdpi

seems to be correct, according to the docs.

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

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).



Related Topics



Leave a reply



Submit