Creating Resources for Different Languages and Hardware in Android

Using the directory structure described here, you can create different resource values for specific languages, locations, and hardware configurations. Android chooses from among these values dynamically at run time using its dynamic resource-selection mechanism.

You can specify alternative resource values using a parallel directory structure within the res folder. A hyphen (-) is used to separate qualifiers that specify the conditions you provide alternatives for

The following example hierarchy shows a folder structure that features default string values, with French language and French Canadian location variations:

Project/
 res/
 values/
 strings.xml
 values-fr/
 strings.xml
 values-fr-rCA/
 strings.xml

The following list gives the qualifiers you can use to customize your resource values:

Mobile Country Code and Mobile Network Code (MCC/MNC) — The country, and optionally the network, associated with the SIM currently used in the device. The MCC is specified by mcc followed by the three-digit country code. You can optionally add the MNC using mnc and the two- or three-digit network code (for example, mcc234-mnc20 or mcc310). You can fi nd a list of MCC/MNC codes on Wikipedia at http://en.wikipedia.org/wiki/ MobileNetworkCode.

Language and Region — Language specified by the lowercase two-letter ISO 639-1 language code, followed optionally by a region specified by a lowercase r followed by the uppercase two-letter ISO 3166-1-alpha-2 language code (for example, en, en-rUS, or en-rGB).

Smallest Screen Width — The lowest of the device’s screen dimensions (height and width) specified in the form swdp (for example, sw600dp, sw320dp, or sw720dp). This is generally used when providing multiple layouts, where the value specified should be the smallest screen width that your layout requires in order to render correctly. Where you supply multiple directories with different smallest screen width qualifiers, Android selects the largest value that doesn’t exceed the smallest dimension available on the device.

Available Screen Width — The minimum screen width required to use the contained resources, specified in the form wdp (for example, w600dp, w320dp, or w720dp). Also used to supply multiple layouts alternatives, but unlike smallest screen width, the available screen width changes to reflect the current screen width when the device orientation changes. Android selects the largest value that doesn’t exceed the currently available screen width

Available Screen Height — The minimum screen height required to use the contained resources, specified in the form hdp (for example, h720dp, h480dp, or h1280dp). Like available screen width, the available screen height changes when the device orientation changes to reflect the current screen height. Android selects the largest value that doesn’t exceed the currently available screen height

Screen Size — One of small (smaller than HVGA), medium (at least HVGA and typically smaller than VGA), large (VGA or larger), or xlarge (significantly larger than HVGA). Because each of these screen categories can include devices with significantly different screen sizes (particularly tablets), it’s good practice to use the more specifi c smallest screen size, and available screen width and height whenever possible. Because they precede this screen size qualifier, where both are specified, the more specific qualifiers will be used in preference where supported

Screen Aspect Ratio — Specify long or notlong for resources designed specifically for wide screen. (For example, WVGA is long; QVGA is notlong.)

Screen Orientation: One of port (portrait), land (landscape), or square (square).

Dock Mode — One of car or desk. Introduced in API level 8

Night Mode — One of night (night mode) or notnight (day mode). Introduced in API level 8. Used in combination with the dock mode qualifi er, this provides a simple way to change the theme and/or color scheme of an application to make it more suitable for use at night in a car dock

Screen Pixel Density — Pixel density in dots per inch (dpi). Best practice is to supply ldpi, mdpi, hdpi, or xhdpi to specify low (120 dpi), medium (160 dpi), high (240 dpi), or extra high (320 dpi) pixel density assets, respectively. You can specify nodpi for bitmap resources you don’t want scaled to support an exact screen density. To better support applications targeting televisions running Android, you can also use the tvdpi qualifi er for assets of approximately 213dpi. This is generally unnecessary for most applications, where including medium- and high-resolution assets is suffi cient for a good user experience. Unlike with other resource types, Android does not require an exact match to select a resource. When selecting the appropriate folder, it chooses the nearest match to the device’s pixel density and scales the resulting Drawables accordingly

Touchscreen Type — One of notouch, stylus, or finger, allowing you to provide layouts or dimensions optimized for the style of touchscreen input available on the host device.

Keyboard Availability — One of keysexposed, keyshidden, or keyssoft

Keyboard Input Type — One of nokeys, qwerty, or 12key.

Navigation Key Availability — One of navexposed or navhidden

UI Navigation Type — One of nonav, dpad, trackball, or wheel

Platform Version — The target API level, specified in the form v (for example, v7). Used for resources restricted to devices running at the specified API level or higher

You can specify multiple qualifiers for any resource type, separating each qualifier with a hyphen. Any combination is supported; however, they must be used in the order given in the preceding list, and no more than one value can be used per qualifier

The following example shows valid and invalid directory names for alternative layout resources.

Table of Contents

VALID

layout-large-land
 layout-xlarge-port-keyshidden
 layout-long-land-notouch-nokeys

INVALID

values-rUS-en (out of order)
 values-rUS-rUK (multiple values for a single qualifier)

When Android retrieves a resource at run time, it finds the best match from the available alternatives. Starting with a list of all the folders in which the required value exists, it selects the one with the greatest number of matching qualifiers. If two folders are an equal match, the tiebreaker is based on the order of the matched qualifiers in the preceding list

Leave a Comment