Archive for 'Pixel'

Display Units in android

Android is an Operating System with high degree of diversity. It has many dives in many forms and sizes. Coding for them is simple if the following units are clearly understood.

px - Pixels – corresponds to actual pixels on the screen.

in - Inches – based on the physical size of the screen.

mm - Millimeters – based on the physical size of the screen.

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

sp - Scale-independent Pixels – this is like the dp unit, but it is also scaled by the user’s font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user’s preference.

To make it absolutely clear – never use any of the above but sp or dp unless you absolutely have to. Using sp / dp will make your Android applications compatible with multiple screen densities and resolutions.

Reference:
http://developer.android.com/guide/topics/resources/more-resources.html#Dimension

Supporting Multiple display resolutions in Android

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)
Density
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
Physical Width 1.5 inches 1.5 inches
Dots Per Inch (“dpi”) 160 240
Pixels (=width*dpi) 240 360
Density (factor of baseline 160) 1.0 1.5
Density-independent Pixels (“dip” or “dp” or “dps”) 240 240
Scale-independent pixels (“sip” or “sp”) Depends on user font size settings same
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView id="@+id/objLbl1"
android:layout_width="134dp"
android:layout_height="80dp"
android:text="Label 1"
/>
<EditText id="@+id/objTxt1"
android:layout_width="134dp"
android:layout_height="134dp"
android:layout_alignTop="@id/objLbl1"
android:layout_toRight="@id/objLbl1"
android:text=""
/>
<Button id="@+id/objBtn1"
android:layout_width="134dp"
android:layout_height="24dp"
android:layout_below="@id/objTxt1"
android:layout_toRight="@id/objLbl1"
android:text="Btn 1"
/>
</LinearLayout>

Always do remember the following:

You can easily ensure that your application will display properly on different screens.

  1. Use wrap_content, fill_parent, or the dp unit (instead of px), when specifying dimensions in an XML layout file
  2. Do not use AbsoluteLayout
  3. Do not use hard coded pixel values in your code
  4. Use density and/or resolution specific resource (ldpi, hdpi mdpi and xhdpi or small, medium, large and xlarge)
We Can Build Your App for You. Lets Get Started !