Thursday, October 24, 2013

Android resource selectors and device independent pixels to support multiple screensizes

Supporting different screensize with Android with resource selectors and device independent pixels
This tutorial describes how to support different screensizes in Android. It covers the usage of device independent pixel and the resource selectors.

1. Defining the size of UI components in layout files

Android allows you to define the size of user interface components in fixed or relative dimensions.
Screens of Android devices are different in terms of resolution and in terms of density. If you use fixed sized dimensions, e.g. pixels, your user interface may look fine on a certain device but the button might be too small on other devices with a higher pixel density.
For example if you specify the pixels directly you might get the following result on a device with a relatively low number of pixels.
Device Independent pixels on low resolution devices
On a device with more pixels using the same number of pixels might lead to an unusable user interface.
Device Independent pixels on low resolution devices
Therefore it is recommended never to use fix-sized dimensions.
The unit of measurement which should be used is dp (which is the same as dip but shorter).
dp refers to the onebase line of an Android device, e.g. 320x480 with 160dpi (dots per inch), which was the size of the first Android device (G1). This size is also known as mdpi (medium dots per inch).
If you specify the size in dp, Android will automatically scale it, depending on the device. On a mdpi device one dp will be similar to one pixel. A dp on ldpi device is smaller (approx. 120dip) on a hdpi device is larger (approx. 240dpi). Therefore a dp occupies approximately the same physical space on every device.
You can use dp in your resources, e.g. layout files.
If the unit should be scale with text preference settings of the user, use the sp unit of measurement. This unit is similar to dp, but it is also scaled by the user preference settings for font size.

2. Defining the size of UI components in source code

The Android API frequently requires that you specify a size in pixel and does not accept dp as input. You need to transform your desired dp into actual pixels.
You can use the following method to calculate the right amount of pixels for a dimension specified indp.
public int convertToPixelFromDp(int input) {
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (input * scale + 0.5f);
}
The density of the for the current device can be queried with the following method call.
getResources().getConfiguration().densityDpi; 

3. Using resource qualifiers

3.1. How do resource qualifiers work?

Android allows you to use resource qualifiers to specify that certain resources should only be used for a specific device configuration (e.g. orientation, resolution, languages). To provide different resource files, e.g. layouts, for a configurations described by a qualifier selector, you create in res folder a new sub-folder using this qualifier, for example for a layout the layout-qualifier folder.

3.2. Using orientation as resource qualifier

Assume for example that you want to a use a special layout for your activity in landscape mode. The layout file is called activity_main.xml. In this case you create the res/layout-land folder and place the new layout file with the same name (activity_main.xml) is this folder. Android select the correct file automatically based on the current configuration.

3.3. Strings and translations

The same way you can provide different resources for your text values, e.g. you create a values-qualifier folder. For example to provide English and German text resources, use the values-en andvalues-de folder.

3.4. Android version qualifiers

A typical selection you use in your application is the selection based on Android version, which is based on the -v[minimum API level] qualifier. For example this way you can provide different styling and themes based on the Android version.

3.5. Width and height related qualifiers

Another typical selection is the smallest available width selection or the available width selection. Thesmallest available width is the shortest of the screen's available height and width which the available width is the actual width based on the orientation of the device.
The width selection can for example be used to provide different layouts based on the width of the device. This selection is based on -sw[Number]dp (Smallest) or -w[Number]dp qualifier, where [Number] stands for the number of device independent pixels. For example a 7inch tablet typically has at least 600dp, and you could provide layouts for it via the res/layout-sw600dp/ selector.
The different resource qualifiers are described on the following webpage.
http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources 


4. Fragments

Fragments give excellent support for multiple screen configurations. See Android Fragments tutorialfor details on Fragments.

No comments:

Post a Comment