If you are starting your journey with android development you may have come across some parameters like DP sp dpi px etc. Let us know what actually these parameters mean, how they work and where to use them.
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
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
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 user's preference.
What is Screen Density and Resolution?
Screen density is not equal to resolution. Screen density is a ratio of resolution and displays size, which can be quantified as dots per inch, or dpi. The higher the dpi, the smaller each individual pixel is, and the greater clarity. Simply put, a higher dpi means more detail is displayed per inch but does not necessarily correlate with a higher screen resolution.
Let me explain it with an example: Galaxy Nexus (4.65" diagonal) has a 720x1280 px resolution, while the Nexus 7 (7" diagonal) has an 800x1280 px resolution. It is a common misconception to assume that they have about the same screen density since their resolutions are almost identical. However, the Galaxy Nexus has a screen density of about 316 dpi and the Nexus 7 has a screen density of 216 dpi, not even close. This is because while they are displaying the same resolution, they are also displaying it in different amounts of space. Again, screen density is a ratio of resolution and display size, and both factors contribute to the density.
Have a look at the tables below for a better understanding.
PX (Pixels), Dpi or Dp (Density Independent Pixels), Sp (Scale independent pixels)
Before getting deeper into this let's generally define them all. px
Pixels - corresponds to actual pixels on the screen.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
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
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 user's preference.
What is Screen Density and Resolution?
Screen density is not equal to resolution. Screen density is a ratio of resolution and displays size, which can be quantified as dots per inch, or dpi. The higher the dpi, the smaller each individual pixel is, and the greater clarity. Simply put, a higher dpi means more detail is displayed per inch but does not necessarily correlate with a higher screen resolution.
Let me explain it with an example: Galaxy Nexus (4.65" diagonal) has a 720x1280 px resolution, while the Nexus 7 (7" diagonal) has an 800x1280 px resolution. It is a common misconception to assume that they have about the same screen density since their resolutions are almost identical. However, the Galaxy Nexus has a screen density of about 316 dpi and the Nexus 7 has a screen density of 216 dpi, not even close. This is because while they are displaying the same resolution, they are also displaying it in different amounts of space. Again, screen density is a ratio of resolution and display size, and both factors contribute to the density.
Have a look at the tables below for a better understanding.
0 Comments