The documentation says that 160 dp (density-independent) equals 1 inch. And 72 pt is also 1 inch. So I don't see why android define a dp measurement while it seems to work the same as points. Can anybody explain that? Why should I use dp if I can use pt?
The Android documentation used to incorrectly state that 160 dp always equals 1 inch regardless of screen density. This was reported as a bug which was accepted and the documentation updated.
From the updated documentation:
160 dp will NOT always equal 1 inch, it will vary with different screen sizes and densities. On a screen with a density of 160dpi (mdpi), 160 dp will equal 1 inches.
1 pt will always equal 1/72 in, regardless of the screen density.
The Android documentation for this is here.
I made a small application to try and verify the different sizes. It looks like what is above is correct, at least when shown on my HTC Aria. Here is a screenshot:
It's interesting to note that these sizes did NOT match up exactly in the eclipse graphical editor. The dp and sp sizes wavered depending on the size of the screen and resolution in the editor. Here are some screenshots from the editor (left is 2.7in QVGA slider, right is 10.1in WXGA, clipped):
It would be interesting to see if these editor renders match up with the actual devices. Can anyone verify these sizes? I'll attach my xml below in case anyone wants to help out.
Edit: Added 2 more devices to John's example. On the left a Samsung Nexus S (OS 2.3.3). On the right, a Samsung Galaxy Tab 10.1 (OS 3.1). No mods.
On the Nexus S, 160dp is slightly larger than 1 inch. All the normal physical units (in, mm, pt) are all the same size. I measured it with a ruler, and the 160 dp bar is about 1mm larger than it should. While the physical units are 1mm shorter than they should.
On the Tab, all bars are exactly the same, and 1mm longer than what I measured with a ruler.
The nuance here is that 160
Take an example, display 160
What about on a 170 dpi device? A 170 dpi device falls into the mdpi density bucket, with a scaler of 1.0.
What about on a 150 dpi device? A 150 dpi device falls into the mdpi density bucket, with a scaler of 1.0.
The moral of the story is,
I have written an in depth blog on Android's dimension units, which gives more info and sample code - Understanding Density Independence in Android
Out of curiosity, I tried the layout from John's answer on my two devices: Asus Transformer (10.1 in) and HTC Legend (3.2 in). The results were pretty interesting:
I've struggled with dimensions but I think I've found a way of looking at it that makes sense to me. There's a conversion formula in Wei-Meng Lee's "Beginning Android 4 Application Development" (pg 111):
Actual pixels = dp * ( dpi / 160 ), where dpi is either 120, 160, 240 or 320
So, using that formula, I can find which dpi for my phone/emulator is closest to one of those four values and that'll determine the ratio (3/4, 1, 3/2 or 2) used in the formula to convert dp to pixels. It's important to me that dpi in the formula can only assume one of those four values despite the actual device pixel density.
Referring to: http://en.wikipedia.org/wiki/List_of_displays_by_pixel_density, for a Nexus S with a pixel density of 235 dpi the conversion is:
pixels = dp * 3/2
So a 160dp button, say, would be 240px (a little wider than an inch with the device's 235 dpi)
For an HTC Legend, with a pixel density of 181 dpi the formula is:
pixels = dp * 1
(because 181 is closest to 160). So that 160dp button would be 160pixels, or slightly less than an inch on the device with its pixel density of 181dpi.
This helps me understand the incorrectness of the previous Android documentation "1 dp will always equal 1/160in, regardless of screen density".
These are the two main points I'm trying to get in my head :)
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".
Developers are used to thinking of things in terms of pixels. Density-independent pixels was Android's way of getting close to what developers are used to. That being said, you are welcome to use points, inches, or millimeters if you prefer.
1 dp will always equal 1/160 in, regardless of screen density.
This is not true as shown by your application... Agree?