55

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?

Herbert
  • 559
  • 1
  • 5
  • 3

7 Answers7

78

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:

HTC Aria resource type test

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):

enter image description here

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.

Samsung Nexus S Samsung Galaxy Tab 10.1

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.

Intrications
  • 16,182
  • 9
  • 47
  • 50
John Leehey
  • 21,128
  • 8
  • 57
  • 85
  • 1
    Pixel is dependent on density, but DP stands for density-independent. From the documentation: "so 160dp is always one inch regardless of the screen density" – Herbert Jul 11 '11 at 21:28
  • Actually, Herbert, I'm not sure my original updated post is correct either. I think 320 dp will always equal 2 inches, regardless of the screen. I'm updating my post again. – John Leehey Jul 11 '11 at 21:35
  • Yes, I was confused. Maybe I've missed something but it seems that 1 DP = 160/72 pt – Herbert Jul 11 '11 at 21:37
  • Yes, I think you're correct. This is interesting to me, I wasn't aware that both variables were density-independent. – John Leehey Jul 11 '11 at 21:43
  • I think I understand de design decision to make a new unit dp. Because smartphones mostly have a much higher density (PPI) then computers. And when using points a font will look much bigger in the dev kit, visual mode then that it will be on the smartphone. – Herbert Jul 12 '11 at 13:34
  • @Herbert, I wanted to verify the resource types on an actual device. Check my updated post. – John Leehey Jul 12 '11 at 17:00
  • 1
    BTW herbert, 1 DP = 72/160 pt :D – John Leehey Jul 12 '11 at 22:27
  • "1 dp will always equal 1/160 in, regardless of screen density." This is incorrect, the Android documentation is wrong. You have shown this yourself with the output from the Eclipse editor. – Intrications Oct 20 '11 at 08:00
  • @Ashton, I was wondering if the eclipse editor was incorrect, because I did not see this result on the device. I know the editor is supposed to use the same rendering engine, but I don't want to say the documentation is wrong unless I get the result on a stock phone rom. – John Leehey Oct 21 '11 at 00:25
  • @Ashton I've updated my answer to better reflect the real-world results of the measurements. – John Leehey Oct 21 '11 at 21:30
  • @John The Android documentation has now been updated to remove the incorrect statement that 160 dp equals 1 inch. I have updated your answer to reflect the new documentation since it is the answer that people will read since it is highest. I have replaced the first part which was incorrect - even the screenshots in the second half of your answer showed this and the Android documentation is now correct. – Intrications Mar 17 '12 at 19:44
  • @Ashton, thanks, I'm glad they got around to clearing up the issue. – John Leehey Mar 19 '12 at 17:15
  • Can you finally conclude which unit to use to make apps campatible to all size devices ?? dp or px or pt?? – Nagappa L M Nov 25 '13 at 06:09
  • @NagappaLM, there is no direct unit to use that magically makes all apps compatible to all size devices. You will need to use your best judgement while following the android layout segregation guidelines. If you want exact measurements (like, exactly 1 inch), use pt, mm or in, but if you want layouts to look similar in readability use dp (or sp for text). Steven's answer explains this well. – John Leehey Nov 25 '13 at 18:48
  • Thanks for your reply – Nagappa L M Nov 27 '13 at 10:41
  • you said that "160 dp will NOT always equal 1 inch, it will vary with different screen sizes and densities" but 160 dp is exactly one inch in every device and monitor all over the cosmos. – EN20 Apr 25 '15 at 08:12
  • On my lg phone, I got 1 inch = 138dp using the following code. inch * metrics.xdpi / metrics.density; –  Feb 09 '20 at 22:32
39

The documentation says that 160 dp (density-independent) equals 1 inch. And 72 pt is also 1 inch.

The nuance here is that 160 dp (or dip) is roughly 1 inch, while 72 pt is exactly 1 inch. The difference is how android converts both units to pixels, which depends on the screen density of the device.


A single dp is a single px on a device at 160 dpi. Android uses the "density bucket" the device falls into, and multiplies a scaler to convert dp to px.

Bucket | DPI | Scaler
---------------------
ldpi   | 120 | 0.75
mdpi   | 160 | 1.00
tvdpi  | 213 | 1.33
hdpi   | 240 | 1.50
xhdpi  | 320 | 2.00
xxhdpi | 480 | 3.00

dp to px converts following this formula: dp * scaler = px.


A single pt is exactly 1/72 of an inch on any screen density. Android converts pt to px using the exact dpi (xdpi and ydpi) of the device's screen.

pt to px converts following this formula: pt / 72 * dpi = px.


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?

Take an example, display 160 dp and 72 pt on a 160 dpi device. A 160 dpi device falls into the mdpi density bucket, with a scaler of 1.0. Use the formulas above to convert to px.

160 dp * 1.0 = 160 px
72 pt / 72 * 160 = 160 px

What about on a 170 dpi device? A 170 dpi device falls into the mdpi density bucket, with a scaler of 1.0.

160 dp * 1.0 = 160 px
72 pt / 72 * 170 = 170 px

What about on a 150 dpi device? A 150 dpi device falls into the mdpi density bucket, with a scaler of 1.0.

160 dp * 1.0 = 160 px
72 pt / 72 * 150 = 150 px

The moral of the story is, dp keeps exact dimensions and helps keep performance, allowing some physical size variation depending on device density. On the other hand, pt is exactly the same physical size on every density, which leads to a different amount of px being used, which can hinder performance and cause aliasing and artifacts if used on images. dp is recommended unless absolutely exact physical dimensions are required (you have a ruler on screen, etc).

I have written an in depth blog on Android's dimension units, which gives more info and sample code - Understanding Density Independence in Android

Steven Byle
  • 12,579
  • 4
  • 41
  • 57
3

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 :)

  1. a range of actual device pixel densities (for example: 141-199) will result in the same ratio (1) in the conversion formula
  2. but unless the pixel density of a particular device is exactly 160 dpi, the 160 dp won't be an inch on the device...close above or below, but not exactly
gcbound
  • 740
  • 8
  • 19
3

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".

Subayan
  • 111
  • 1
  • 1
  • 7
3

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):

Transformer

And Legend:

Legend

howettl
  • 11,761
  • 13
  • 54
  • 88
  • This is really interesting to me because I was under the impression that the HTC Legend was the same as the HTC Aria... weird!! – John Leehey Jul 12 '11 at 17:22
  • Nevermind... I just realized that I was using a liberated Legend 2.2 ROM and though they were the same because of that :P – John Leehey Jul 12 '11 at 17:24
  • Funny. According to [Wikipedia](http://en.wikipedia.org/wiki/HTC_Aria): the HTC Aria has "the same software introduced on the HTC Desire and HTC Legend" but the Legend doesn't have capacitive buttons. I wonder what causes the difference? I'm running Cyanogenmod on my Legend. – howettl Jul 12 '11 at 17:28
  • It must have something to do with the ROM then. I can try the Cyanogenmod rom on my aria, I have it already on the SD, but I won't be able to for a while. – John Leehey Jul 12 '11 at 17:42
  • The Transformer has a dpi of 160 so 160dp really is 1 inch in this case so all the lines are the same length. – Intrications Mar 17 '12 at 19:45
2

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.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • 1
    However, if you do prefer to use points, inches, or millimeters, you will have more problems scaling your app to different screen sizes. – Intrications Oct 19 '11 at 22:06
  • @AshtonBRSC why is it so? say I divide all my `dip`s by 160 and change `dip` to `inch`, what's the difference than ? – Pacerier Oct 21 '11 at 08:46
  • @Pacerier I think he means that if you make all your sizes in terms of inches, they will always remain that size. In other words, a 1-inch button on a tiny 3.5" phone will still be 1 inch on a 10-inch tablet (where you might want the button to scale a little larger). – John Leehey Oct 21 '11 at 21:31
  • @John I mean if I divide all my `dip`s by 160 and multiple it by 72 and change `dip` to `pt`, what's the difference than ? – Pacerier Oct 22 '11 at 13:22
  • 1
    @Pacerier, according to the android documentation, there is no difference, but if you read my and howettl's answer, there is apparently some sort of discontinuity between what the screen is supposed to display and what it actually displays for some devices. – John Leehey Oct 24 '11 at 17:05
  • @John yes that's what I was saying. Is it the `dip` that has errors or is it the `pt` that has errors? – Pacerier Oct 25 '11 at 04:23
  • @Pacerier, to tell the truth, I don't know, because from the screenshots you can't tell which is actually the measure of 1 inch. `pt` is the one that consistently matches up with `in` though. – John Leehey Oct 25 '11 at 17:46
  • @John so to rephrase your comment above, you mean that `pt` is prefered over `dip` ? – Pacerier Oct 26 '11 at 12:46
  • @Pacerier, what is "preferred" is up to the user, but what is most accurate is undecided at this point because there are no real-world comparisons on these posts. For instance, in howettls post, how do we know that the `dp` measurement isn't the one that equals a real inch, and the `pt` and `in` measurements aren't wrong? Needs a ruler... but assuming the `in` measurement is always right, then it appears that `pt` will be more accurate for exact-length measurements. – John Leehey Oct 27 '11 at 17:46
  • @John I mean what's wrong with using a ruler? I launched the emulator as "exact size" so it should be also exactly one inch for you on screen right? – Pacerier Oct 28 '11 at 06:17
  • 1
    @Pacerier, nothings wrong with using a ruler, I just don't have one... if you want to verify whats right and whats wrong go ahead! – John Leehey Oct 28 '11 at 22:03
0

1 dp will always equal 1/160 in, regardless of screen density.

This is not true as shown by your application... Agree?

BR STeN

STeN
  • 6,172
  • 21
  • 75
  • 121
  • You are correct, 1 dp will vary depending on the device. The Android documention is wrong when it says "so 160dp is always one inch regardless of the screen density". – Intrications Oct 19 '11 at 22:11
  • It is awful to make the application running correct on various tablets and OS versions... The iPad is much easier from this point of view... – STeN Oct 20 '11 at 04:33
  • 3
    iOS is easier at the moment but Apple is limited in what screen resolutions it can offer using their current layout system. That's why the iPhone 4 had a resolution exactly twice the size - so the same layouts with absolute positioning can be used. iPhone layouts cannot be reused on the iPad but, with Android, phone layouts can be used on tablets. Android is much more flexible, just needs a bit more work. And it would help if the documentation didn't have errors like this. – Intrications Oct 20 '11 at 07:56
  • @STeN so are you saying that we should use inch or dp ? – Pacerier Oct 21 '11 at 08:50
  • @Pacerier Never use inch unless you're doing something like putting a ruler on the screen that really has to be a certain size in inches. Android screen sizes vary from around 2.5 inches up to over 10 inches so you will end up with layouts that look terrible on most screen sizes. Basically, just always use dp and sp unless you know why you are using a different unit of size. – Intrications Nov 02 '11 at 16:37