Android support different units of measures:
- dp
- sp
- px
- pt
- mm
- in
They recommend using dp for layout and sp for font size.
Can someone point me to a use case where I should be using one of those: px, pt, mm and in ?
Android support different units of measures:
They recommend using dp for layout and sp for font size.
Can someone point me to a use case where I should be using one of those: px, pt, mm and in ?
A dimension value 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:
1.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.
2.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.
3.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.
4.mm
Millimeters - Based on the physical size of the screen.
5.in
Inches - Based on the physical size of the screen.
To better understand dip/dp and why use them watch this video.
You dont need to use direct values of sp in android. You can use textAppearance attribute for predefined values. Here is some info about textAppearance.
Here you have similar question with detailed answers.
Pixels, inches, millimeters and points are units which will be differently displayed on different screens. Screen resolutions can vary between 240x320px and 2560x1600, even with similar screen size and vice versa. Use them only when you absolutely have to (e.g. ruler app).