What is the difference between px, dip, dp, and sp?
From the Android Developer Documentation:
px
Pixels - corresponds to actual pixels on the screen.in
Inches - based on the physical size of the screen.
1 Inch OR 2.54 centimetersmm
> Millimeters - based on the physical size of the screen.pt
> Points - 1/72 of an inch based on the physical size of the screen.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".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 whatsp
actually stands for, one doc says "scale-independent pixels", the other says "scaleable pixels".
From Understanding Density Independence In Android:
Density Bucket | Screen Density | Physical Size | Pixel Size |
---|---|---|---|
ldpi | 120 dpi | 0.5 x 0.5 in | 0.5 in * 120 dpi = 60x60 px |
mdpi | 160 dpi | 0.5 x 0.5 in | 0.5 in * 160 dpi = 80x80 px |
hdpi | 240 dpi | 0.5 x 0.5 in | 0.5 in * 240 dpi = 120x120 px |
xhdpi | 320 dpi | 0.5 x 0.5 in | 0.5 in * 320 dpi = 160x160 px |
xxhdpi | 480 dpi | 0.5 x 0.5 in | 0.5 in * 480 dpi = 240x240 px |
xxxhdpi | 640 dpi | 0.5 x 0.5 in | 0.5 in * 640 dpi = 320x320 px |
Difference between dp and sp (The other definitions that I have seen was still confusing me)?
1 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:
px
Pixels - corresponds to actual pixels on the screen.in
Inches - based on the physical size of the screen.
1 Inch OR 2.54 centimetersmm
> Millimeters - based on the physical size of the screen.pt
> Points - 1/72 of an inch based on the physical size of the screen.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".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 whatsp
actually stands for, one doc says "scale-independent pixels", the other says "scaleable pixels".
From Understanding Density Independence In Android:
Density Bucket | Screen Density | Physical Size | Pixel Size |
---|---|---|---|
ldpi | 120 dpi | 0.5 x 0.5 in | 0.5 in * 120 dpi = 60x60 px |
mdpi | 160 dpi | 0.5 x 0.5 in | 0.5 in * 160 dpi = 80x80 px |
hdpi | 240 dpi | 0.5 x 0.5 in | 0.5 in * 240 dpi = 120x120 px |
xhdpi | 320 dpi | 0.5 x 0.5 in | 0.5 in * 320 dpi = 160x160 px |
xxhdpi | 480 dpi | 0.5 x 0.5 in | 0.5 in * 480 dpi = 240x240 px |
xxxhdpi | 640 dpi | 0.5 x 0.5 in | 0.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
ordip
means Density-independent Pixelsdpi
orppi
means Dots (or Pixels) Per Inchinch
is a physical measurement connected to actual screen sizepx
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
CSS for a Sidebar Menu That Folds in and Out
How to Use > or < (Greater Than and Less Than ) Symbols in Media Queries
Media Queries - CSS Only for iPhone Landscape
How to Clear The Last <Li> Tag Within a <Ul>
React Native - Why Padding Does Not Work
Input[Type='Text'] CSS Selector Does Not Apply to Default-Type Text Inputs
Overriding Bootstrap Table-Striped CSS
Detecting Printed Page Size with CSS Media Queries
Z-Index Ie8 Bug on Generated Content with: After
Add Class to an Element in Angular 4
What's The Difference Between Line-Height:1.5 and Line-Height:150% in CSS
What Is The Replacement for The Child Selector
CSS: Define Media Query Within One Class
Difference Between Ng-Class and Ng-Style
Using CSS Variables as SASS Function Arguments