in
Inches - based on the physical size of the screen.
1 Inch = 2.54 centimeters

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

pt (it depends upon physical size of the screen)
Points - 1/72 of an inch based on the physical size of the screen.

This will help you for more understanding.

dp
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
Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.

To make it absolutely clear - try to never use anything but sp or dp unless you absolutely have to. Using sp/dp will make your Android applications compatible with multiple screen densities and resolutions.
–
Daniel LewJan 8 '10 at 5:01

16

Well i've just encounted an issue where by using dp rather than dip meant my TextView in LinearList didn't add any padding. Using dip worked, dp didn't. Go figure.
–
EmileDec 22 '10 at 11:05

68

One note about db/sp that isn't totally obvious: The scaling that occurs for these depends not on the devices real density (dpi) but on which "bucket" of densities it falls into: available buckets are: 120,160,240,320. This can cause some problems handling screens that are significantly different but get bucketed the same.
–
FraggleOct 29 '11 at 4:10

12

Is there a reason for not using sp for everything?
–
WarpzitNov 16 '11 at 11:28

36

@Warpzit, yes, using sp for anything but text would be wrong. In "large text" accessibility mode, the text is supposed to get "fatter" relative to the rest of the UI, rather than just the whole UI scaling up.
–
poolieJan 29 '12 at 7:49

If you are any serious about developing an android app for more than one type of device, you should have read the above at least once. In addition to that it is always a good thing to know the actual number of active devices that have a particular screen configuration.

So if you use dp for a button and sp for the font size of the button text, what happens when the user starts scaling? The text will enlarge, but will the button accommodate this by enlarging also?
–
WytzeOct 5 '12 at 9:35

5

@Wytze, nope. For things where fitting the text may be an issue, I would just use dip so things don't overflow.
–
eskiJul 13 '13 at 17:39

2

@Wytze And I, on the contrary, use sp for both the text and whatever it contains. For example, if I have a button with a fixed size and a text inside it, I would assign the button size in sp so it scales up when needed too. Otherwise, the user will be annoyed that he increased the size of the text, and it didn't increase. Of course, the layout should be flexible enough to allow it.
–
MalcolmOct 14 '13 at 23:15

@Malcolm it's not always possible to have a design that guarantees this. esp for complex designs.
–
SamMar 28 '14 at 2:07

@Sam Well, it's up to you to decide what is more important: your design or the ability for users with vision problems to read your text.
–
MalcolmMar 28 '14 at 2:13

px
Pixels - point per scale corresponds to actual pixels on the screen.

in
Inches - based on the physical size of the screen.

mm
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
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
Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.

Take the example of two screens that are the same size but one has a resolution of 160 dpi (dots per inch, i.e. pixels per inch) and the other is 240 dpi.

Screen density: The quantity of pixels within a physical area of the screen; usually referred to as dpi (dots per inch). For example, a
"low" density screen has fewer pixels within a given physical area,
compared to a "normal" or "high" density screen. For simplicity,
Android groups all actual screen densities into four generalized
densities: low, medium, high, and extra high.

Orientation: The orientation of the screen from the user's point of view. This is either landscape or portrait, meaning that the
screen's aspect ratio is either wide or tall, respectively. Be aware
that not only do different devices operate in different orientations
by default, but the orientation can change at runtime when the user
rotates the device.

Resolution: The total number of physical pixels on a screen. When adding support for multiple screens, applications do not work directly
with resolution; applications should be concerned only with screen
size and density, as specified by the generalized size and density
groups.

Density-independent pixel (dp): A virtual pixel unit that you should use when defining UI layout, 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, which is the
baseline density assumed by the system for a "medium" density screen.
At runtime, the system transparently handles any scaling of the dp
units, as necessary, based on the actual density of the screen in use.
The conversion of dp units to screen pixels is simple: px = dp * (dpi
/ 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical
pixels. You should always use dp units when defining your
application's UI, to ensure proper display of your UI on screens with
different densities.

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.

Use of dp:

Density independence -
Your application achieves “density independence” when it preserves the physical size (from the user’s point of view) of user interface elements when displayed on screens with different densities. (ie) The image should look the same size (not enlarged or shrinked) in different types of screens.

sp

Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference.

These are dimension values defined in XML. A dimension is specified
with a number followed by a unit of measure. For example: 10px, 2in,
5sp. The following units of measure are supported by Android:

dp

Density-independent Pixels - An abstract unit that is based on the
physical density of the screen. These units are relative to a 160 dpi
(dots per inch) screen, on which 1dp is roughly equal to 1px. When
running on a higher density screen, the number of pixels used to draw
1dp is scaled up by a factor appropriate for the screen's dpi.
Likewise, when on a lower density screen, the number of pixels used
for 1dp is scaled down. The ratio of dp-to-pixel will change with the
screen density, but not necessarily in direct proportion. Using dp
units (instead of px units) is a simple solution to making the view
dimensions in your layout resize properly for different screen
densities. In other words, it provides consistency for the real-world
sizes of your UI elements across different devices.

sp

Scale-independent Pixels - This is like the dp unit, but it is also
scaled by the user's font size preference. It is recommend you use
this unit when specifying font sizes, so they will be adjusted for
both the screen density and the user's preference.

pt

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

px

Pixels - Corresponds to actual pixels on the screen. This unit of
measure is not recommended because the actual representation can vary
across devices; each devices may have a different number of pixels per
inch and may have more or fewer total pixels available on the screen.

mm

Millimeters - Based on the physical size of the screen.

in

Inches - Based on the physical size of the screen.

Note: A dimension is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine dimension resources with other simple resources in the one XML file, under one element.

Difference between dp and sp units mentioned as "user's font size preference" by the answers copied from official documentation can be seen at run time by changing Settings->Accessibility->Large Text option.

Density-independent pixel (dp)
A virtual pixel unit that you should use when defining UI layout, to express layout
dimensions or position in a density-independent way. As described above, the density-independent pixel is equivalent to one physical pixel on a 160 dpi screen,
which is the baseline density assumed by the system for a "medium" density screen. At
runtime, the system transparently handles any scaling of the dp units, as necessary, based
on the actual density of the screen in use. The conversion of dp units to screen pixels is
simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical
pixels. You should always use dp units when defining your application's UI, to ensure
proper display of your UI on screens with different densities.

Understanding pixel to dp & vice versa is very essential (Especially for giving exact dp values to creative team )

Try to get all pixel values in even numbers from creative team. Otherwise precision lose will happen while multiplying with 0.5.

px
It is explained above. Try to avoid in layout files. But there are some cases, where px is required. For eg, ListView divider line. px is better here for giving one pixel line as divider for all across screen resolutions.

sp
Use sp for Font sizes. Then only the font inside the application will change while device fonts size changes ( i.e. Display -> Fonts on Device ). If you want to keep a static sized font inside the app, you can give font dimension in dp. In such case, it will never change. Developers may get such requirement for some specific screens, for that, developers can use dp instead of sp. In all other cases, sp is recommended.

There is a lot of talk about using "sp" for font sizes, and while i appreciate the point, i don't think that it is the right thing to do from a design point of view. You can end up breaking your design if the user has some wonky font size selection, and the user will end up blaming the app, and not their own life choices.

Also, if you take an sp-font app on a 160dpi tablet, you will find that everything scales up... but your font, which is going to look tiny in comparison. It isn't a good look.

While the idea of "sp" fonts has a good heart, it is a poor idea. Stick with dp for everything.

I have answered relative to layout perspective.. Please read on link you provided "so you should use this measurement unit when defining text size (but never for layout sizes)."
–
KushalMar 20 at 10:22

The question was "Difference between px, dp, dip and sp in Android?" your answer said "We should avoid to use sp". There was no mention of "layout perspective" anywhere.
–
ci_Mar 20 at 10:28