Tutorial details

Android resource selectors and device independent pixels | App Code for Sale | Preview

Android resource selectors and device independent pixels | Android Tutorial

Supporting different screensize with Android with resource selectors and device independent pixels.

Overview PAGE TOP

Lars Vogel

Version 10.6

Copyright © 2009, 2010, 2011, 2012, 2013 Lars Vogel

01.01.2013

vogellafan.6.1.JPG

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 PAGE TOP

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.

vogellafan.6.2.png

On a device with more pixels using the same number of pixels might lead to an unusable user interface.

vogellafan.6.3.png

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 PAGE TOP

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 in dp.

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.1. How do resource qualifiers work? PAGE TOP

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 PAGE TOP

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

3.3. Strings and translations PAGE TOP

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 and values-de folder.

3.4. Android version qualifiers PAGE TOP

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 PAGE TOP

Another typical selection is the smallest available width selection or the available width selection. The smallest 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 PAGE TOP

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

Reference PAGE TOP

http://www.vogella.com/articles/AndroidDifferentScreensize/article.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.