Home Tutorials Training Consulting Products Books Company Donate Contact us









NOW Hiring

Quick links

Share

Android resource selectors. This tutorial describes how to the resource selectors work in Android. It describes how to support different screen sizes, languages and densities on different devices.

1. Resources and Android device configurations

Android devices comes in a variety of different configurations in the sense of size, screen pixel density, language settings, etc. Android supports the automatic selection of resources fitting to the device configuration.

For this, you provide different resources in pre-defined sub-folders in the res directory. The suffix name of a sub-folder defines the device configuration for which it is valid, e.g., orientation, resolution, languages).

1.1. Important resource qualifiers

1.1.1. Screen density

Screens of Android devices are different in terms of resolution and in terms of density of the pixels on the screen. In case of a different screen densities the same length of a user interface component consists of a different amount of pixels.

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

Using the same number of pixels on a device with more pixels might lead to an unusable graphical user interface.

Device independent pixels on low resolution devices

For this reason you should provide graphical resources, e.g., the laucher and action bar icons in different resolutions. This can be done by using resource qualifiers based on the pixel desity.

You can use the density as resource qualifier. Android support the following selections. The baseline is 160 dots per inch (dpi). If for example the Android device has 320 dpi, the Drawable from the drawable-hdpi is selected.

Table 1. Density resource selector
Density selector Equals

ldpi

160 dpi x 0.75

mdpi

160 dpi

hdip

1.5 x 160 dpi = 240 dpi

xhdpi

2 x 160 dpi = 320 dpi

xxhdpi

3 x 160 dpi = 480 dpi

xxxhdpi

4 x 160 dpi = 640 dpi

1.1.2. Icon sizes

You should provide your launcher, action bar and notification bar icons in the important five size.

Table 2. Android icons size
Icons mdpi hdpi xhdpi xxhdpi xxxhdpi

Launcher icon

48 px

72 px

96 px

144 px

192 px

Action bar icon

32 px

48 px

64 px

96 px

128 px

Notification icon

24 px

36 px

48 px

72 px

96 px

The launch icon should also be provide in 512x512 px for the published application in Google Play.

1.1.3. Using orientation as resource qualifier

Assume, for example, that you want to use a special layout for your activity in landscape mode and that 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) in this folder.

1.1.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 API revision.

A typical example for using this qualifier is to define the styling based on the Android version.

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

1.1.6. More resource qualifiers

The other resource qualifiers are described on the following webpage.

http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources

1.2. Strings and translations

1.2.1. Strings and translations

You can also use resource selectors for your values folder with contains, for example, your string values. Use the values-qualifier folder for this.

For example, to provide English and German text resources, use the values-en and values-de folder.

1.2.2. Plurals

Android supports Plurals. Plurals are XML based resources which allow to handle different quantities. This way you can select the right text based on the quantity. In your XML file you specify values for the quantities “zero”, “one”, “two”, “many”, “few”, “many”, “other” and in your code you use the getQuantityString() method to get the correct value. You can also format strings. If now Strings are formated then you pass in the plural resources and the number. If Objects should be used for formating you pass them as additional parameters.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <plurals
        name="tutorials">
        <item quantity="zero">no Tutorial </item>
        <item quantity="one">one Tutorial </item>
        <item quantity="other">%d  Tutorials</item>
    </plurals>
</resources>

For example the following will define a plural. This file needs to be in the “res/values” directory and in this example it is called “plurals”.

// number is defined somewhere before this
// number =....
// get the Resources
Resources res = getResources();
// get the
String quantityString = res.getQuantityString(R.plurals.tutorials,
    number, number);
// do something with it...

Note that the selection is made based on grammatical necessity. A string for zero in English will be ignored even if the quantity is 0, because 0 isn’t grammatically different from 2, or any other number except 1 (“no tutorial”, “one tutorial”, “two tutorials”, and so on).

1.2.3. Using Google translate

You can either translate your String manually (either yourself or a paid organization) or use a machine translation.

White a machine based translation is not optional it might be a good starting point or the only option if the budget for the application is limited. Google Translate provides free machine translation with the option to hire professionals for checks.

The tool offer free suggestions but it first offers you (at the moment) a paid translation. Click through this to get to the machine translation.

2. Defining the size of UI components in layout files

2.1. Fixed or relative dimensions

Android allows you to define the size of user interface components in fixed or relative dimensions in the layout files. Always use relative dimensions in your Android application, as this allows your UI to adapt to the actual device resolution.

2.2. Using dp as relative dimension

The unit of measurement which should be used is dp.

dp is short for dip (device independent pixel).

dp refers to one pixel on an Android device with 160dpi (dots per inch). This is the density of the first available Android device (G1). This size is also known asmdpi (medium dots per inch).

If you specify the size in dp, Android automatically scale your user interface component, depending on the device.

On a mdpi (a.k.a. medium density) device one dp is the same as one pixel. A dp on ldpi (low density) device is smaller (approx. 120dip), on a hdpi (high density) 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.

2.3. Using sp to scale with the user text preferences

If the unit should scale with text preference settings of the user, choose the sp unit of measurement. This unit is similar to dp, but it is also scaled by the user preference.

If the users select to increase the font size in this settings, views which use sp are scaled accordingly.

2.4. Defining the size of UI components in source code

The Android API frequently requires you to specify a size in pixels and does not accept dp as input. In this case 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 dpInput) {
    // get the screen's density scale
    final float scale = getResources().getDisplayMetrics().density;
    // convert the dps to pixels, based on density scale
    return (int) (dpInput * scale + 0.5f);
}

The density for the current device can be queried with the following method call.

getResources().getConfiguration().densityDpi;

3. Fragments

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

4. About this website

5. Resources Supporting different screen sizes

5.1. vogella GmbH training and consulting support

TRAINING SERVICE & SUPPORT

The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”.

The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.

Copyright © 2012-2016 vogella GmbH. Free use of the software examples is granted under the terms of the EPL License. This tutorial is published under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany license.

See Licence.