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
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".
1 dp will always equal 1/160 in, regardless of screen density.
This is not true as shown by your application... Agree?
BR STeN
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 :)
Why should I use dp if I can use pt?
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.
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:
Transformer (cropped):
And Legend:
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.
UPDATE:
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.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="22sp" android:padding="5sp" android:text="160dp"></TextView>
<View android:id="@+id/view1" android:layout_height="20dip" android:layout_width="160dp" android:layout_marginLeft="20sp" android:background="#FF22FF22"></View>
<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="22sp" android:padding="5sp" android:text="72pt"></TextView>
<View android:id="@+id/view2" android:layout_height="20dip" android:layout_width="72pt" android:layout_marginLeft="20sp" android:background="#FF22FF22"></View>
<TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="22sp" android:padding="5sp" android:text="1in"></TextView>
<View android:id="@+id/View01" android:layout_height="20dip" android:layout_width="1in" android:layout_marginLeft="20sp" android:background="#FF22FF22"></View>
<TextView android:layout_width="wrap_content" android:textSize="22sp" android:layout_height="wrap_content" android:text="160sp" android:padding="5sp" android:id="@+id/TextView01"></TextView>
<View android:layout_marginLeft="20sp" android:layout_width="160sp" android:id="@+id/View04" android:background="#FF22FF22" android:layout_height="20dip"></View>
<TextView android:layout_width="wrap_content" android:textSize="22sp" android:layout_height="wrap_content" android:padding="5sp" android:id="@+id/TextView02" android:text="160px"></TextView>
<View android:layout_marginLeft="20sp" android:id="@+id/View03" android:background="#FF22FF22" android:layout_height="20dip" android:layout_width="160px"></View>
<TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="22sp" android:padding="5sp" android:text="25.4mm"></TextView>
<View android:id="@+id/View02" android:layout_height="20dip" android:layout_marginLeft="20sp" android:background="#FF22FF22" android:layout_width="25.4mm"></View>
</LinearLayout>
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.