Android: what is the difference between resolution and density?

Resolution is a measure of how many pixels a display can show at a time eg. 640x480. Density is a measure of how big each pixel is in actual units. This relates heavily to screen size eg. 640x480 on a 15' display.

When comparing one device's resolution to another, it's a straight-forward comparison: the higher the resolution, the more information you can display at a time. When comparing density: the higher the density the sharper the display will appear.

If you have high resolution and low density, then your screen will be very large. On the other hand, low resolution and high density will result in a very small screen.


Best Practices


The objective of supporting multiple screens is to create an application that can function properly and look good on any of the generalized screen configurations supported by Android. The previous sections of this document provide information about how Android adapts your application to screen configurations and how you can customize the look of your application on different screen configurations. This section provides some additional tips and an overview of techniques that help ensure that your application scales properly for different screen configurations.

Here is a quick checklist about how you can ensure that your application displays properly on different screens: 1.Use wrap_content, fill_parent, or dp units when specifying dimensions in an XML layout file 2.Do not use hard coded pixel values in your application code 3.Do not use AbsoluteLayout (it's deprecated) 4.Supply alternative bitmap drawables for different screen densities

The following sections provide more details.

  1. Use wrap_content, fill_parent, or the dp unit for layout dimensions

When defining the android:layout_width and android:layout_height for views in an XML layout file, using "wrap_content", "fill_parent" or dp units guarantees that the view is given an appropriate size on the current device screen.

For instance, a view with a layout_width="100dp" measures 100 pixels wide on medium-density screen and the system scales it up to 150 pixels wide on high-density screen, so that the view occupies approximately the same physical space on the screen.

Similarly, you should prefer the sp (scale-independent pixel) to define text sizes. The sp scale factor depends on a user setting and the system scales the size the same as it does for dp.

  1. Do not use hard-coded pixel values in your application code

For performance reasons and to keep the code simpler, the Android system uses pixels as the standard unit for expressing dimension or coordinate values. That means that the dimensions of a view are always expressed in the code using pixels, but always based on the current screen density. For instance, if myView.getWidth() returns 10, the view is 10 pixels wide on the current screen, but on a device with a higher density screen, the value returned might be 15. If you use pixel values in your application code to work with bitmaps that are not pre-scaled for the current screen density, you might need to scale the pixel values that you use in your code to match the un-scaled bitmap source.

If your application manipulates bitmaps or deals with pixel values at runtime, see the section below about Additional Density Considerations.

  1. Do not use AbsoluteLayout

Unlike the other layouts widgets, AbsoluteLayout enforces the use of fixed positions to lay out its child views, which can easily lead to user interfaces that do not work well on different displays. Because of this, AbsoluteLayout was deprecated in Android 1.5 (API Level 3).

You should instead use RelativeLayout, which uses relative positioning to lay out its child views. For instance, you can specify that a button widget should appear "to the right of" a text widget.

  1. Use size and density-specific resources

Although the system scales your layout and drawable resources based on the current screen configuration, you may want to make adjustments to the UI on different screen sizes and provide bitmap drawables that are optimized for different densities. This essentially reiterates the information from earlier in this document.

If you need to control exactly how your application will look on various screen configurations, adjust your layouts and bitmap drawables in configuration-specific resource directories. For example, consider an icon that you want to display on medium and high density screens. Simply create your icon at two different sizes (for instance 100x100 for medium density and 150x150 for high density) and put the two variations in the appropriate directories, using the proper qualifiers: res/drawable-mdpi/icon.png //for medium-density screensres/drawable-hdpi/icon.png //for high-density screens


Basically:

  1. Resolution concerns an absolute number of pixels. (check out wikipedia Image Resolution)
  2. Density (aka Pixels per inch - PPI) concerns a relative number of pixels per inch. (check out the wikipedia Pixel Density)

Take care, Beco


Resolution is about how many pixels you can show on screen.

Density is based on your device real size, if it's small and has a higher resolution, than the density is high cause you show more pixels in less physical space.