Android is designed to run on a variety of devices that offer a range of screen sizes and resolutions. For applications, the platform provides a consistent environment across devices and handles much of the complexity of adapting an application’s UI to the screen on which it is being displayed. At the same time, the platform exposes APIs that give application developers precise control over their application’s UI when displayed on specific screen sizes and resolutions.
One way of attaining screen interdependency is by using Density Independent Pixels(dp or dip)
Based on the screen resolution, the spread of pixels across the physical width and height of the screen.
A screen with lower density has fewer available pixels spread across the screen , while a screen with higher density has more — sometimes significantly more — pixels spread across the same area. The density of a screen is important because, other things being equal, a UI element (such as a button) whose height and width are defined in terms of screen pixels will appear larger on the lower density screen and smaller on the higher density screen. Uniqueness of the GUI gets disturbed.
Instead of Using Pixels within an app, dp can be used. using dp makes the android system automatically adjust the screen by scaling and resizing. The android system also selects the best fit possible for the application automatically.
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, the baseline density assumed by the platform (as described later in this document). At run time, the platform transparently handles any scaling of the dp units needed, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: pixels = dps * (density / 160).
For example, consider a 240 dpi screen, 1 dp would equal 1.5 physical pixels. Using dp units to define your application’s UI is highly recommended, as a way of ensuring proper display of your UI on different screens.
||Lower resolution screen
||Higher resolution, same size
|Dots Per Inch (“dpi”)
|Density (factor of baseline 160)
|Density-independent Pixels (“dip” or “dp” or “dps”)
|Scale-independent pixels (“sip” or “sp”)
||Depends on user font size settings
<?xml version="1.0" encoding="utf-8"?>
Always do remember the following:
You can easily ensure that your application will display properly on different screens.
- Use wrap_content, fill_parent, or the dp unit (instead of px), when specifying dimensions in an XML layout file
- Do not use AbsoluteLayout
- Do not use hard coded pixel values in your code
- Use density and/or resolution specific resource (ldpi, hdpi mdpi and xhdpi or small, medium, large and xlarge)