Difference between android dimension: pt and dp

pt to dp converter
css dp
calculate dp android
android layout dp vs px
android dpi
scale independent pixels
android:drawable size
screen density

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.

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.

Difference Between dp, dip, sp, px, in, mm, pt in Android, The number of pixels that fit into an inch is referred to as pixel density. When developing an Android app, use dp to display elements uniformly on The primary difference between an sp and a dp is that sp's preserve a user's font settings. pt: 1/72 of an inch, with respect to the physical screen size. Always use dp and sp only. sp for font sizes and dp for everything else. It will make UI compatible for Android devices with different densities. Formula for Conversion between Units px = dp * (dpi / 160) dp to px in device. Following example may help understand better.

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

Android App Development Tutorial 8: Difference in dp, sp, px, pt , You probably should send your designs with pixel measurements because pixels are easier to convert to density independent pixels (dp). In Android, we have a baseline density of 160 dots-per-inch (dpi). So, for a 160 dpi screen, we have 1 pixel = 1 dp and 320 dpi screen, we have 2 pixels = 1 dp which is 2x. px = dp * (dpi / 160) Let’s say we have a tablet of 1280*800 pixels, 160 dpi and phone of 800*1280 pixels, 320 dpi. Their pixel resolution is the same.

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:

#1.2 Android Multi Screen support: dp vs. dpi vs. px vs. sp. How 'dp , dp or dip (just use dp in your code if you're cool) stands for density-independent pixels. What is the difference between the two? None! They both  Learn what is the difference between dp (density independent pixels), sp (scale independent pixels), etc. -----

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

Pixel density, Enter a value and unit to calculate the dimensions for the various DPI bins (ldpi, The primary DPI bins in Android: ldpi, mdpi, hdpi, xhdpi, xxhdpi and xxxhdpi  What is the difference between px sp dp dip in android. dp : Full form of dp is density-independent pixel. dp is a type of abstract unit based upon physical density size of android mobile screen. According to Google android developers 1dp is mostly equal to 1 px. dpi : Full form of dpi is Dot per inch.

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

Is dp to pt conversion ratio 1:1?, Android Units of Measurements - Difference between px, in, mm, dp, dip and sp Physical size of the screen in millimeters. pt (Points) - 1/72 of an inch. dp  SP Android. DP Android . Full Form. Scale Independent Pixel. Density Independent Pixel. Definition. SP is similar to the DP unit but additionally it is also scaled according to the font size preference of the user. DP is an abstract or virtual unit which is based on the physical density of the screen. One DP means one pixel on a 160 dpi screen

Android SP vs DP - Ray Acevedo, pt. Points - 1/72 of an inch based on the physical size of the screen. dp or dip. Density-independent From Understanding Density Independence In Android: Android 3.2 introduces a new approach to screen sizes,the numbers describing the screen size are all in “dp” units.Mostly we can use. smallest width dp: the smallest width available for application layout in “dp” units; this is the smallest width dp that you will ever encounter in any rotation of the display.

DP/PX converter, 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.

Android Units of Measurements , Designing Android app you have to be aware of enormous variety of devices. They have different physical sizes (from small phones to large tablets), different screen sizes (diagonal from 3 inches or less to above 5 inches), different screen resolutions (from 320x480 pixels to Full HD – 1920x1080 – or more) and different screen densities, so number of pixels per inch (from 120 dpi to 480 dp).

Comments
  • The Android documentation has now been updated to remove the incorrect statement that 160 dp equals 1 inch.
  • 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"
  • 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.
  • Yes, I was confused. Maybe I've missed something but it seems that 1 DP = 160/72 pt
  • Yes, I think you're correct. This is interesting to me, I wasn't aware that both variables were density-independent.
  • BTW herbert, 1 DP = 72/160 pt :D
  • what about 5.1 inch to 5.4 inch(sw480dp)
  • @cammando physical size != sw###dp != dpi. Give my blog a read, it should help you understand the differences.
  • This is really interesting to me because I was under the impression that the HTC Legend was the same as the HTC Aria... weird!!
  • Nevermind... I just realized that I was using a liberated Legend 2.2 ROM and though they were the same because of that :P
  • Funny. According to Wikipedia: 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.
  • 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.
  • The Transformer has a dpi of 160 so 160dp really is 1 inch in this case so all the lines are the same length.
  • However, if you do prefer to use points, inches, or millimeters, you will have more problems scaling your app to different screen sizes.