One of the most compelling reasons to externalize your resources is Android's dynamic resource-selection mechanism.
Using the directory structure described below, you can create different resource values for specific languages, locations, and hardware configurations. Android will choose from among these values dynamically at run time.
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're providing alternatives for.
The following example hierarchy shows a folder structure that features default string values, with French language and French Canadian location variations:
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 (e.g., mcc234-mnc20 or mcc310). You can find a list of MCC/MNC codes on Wikipedia at http://en.wikipedia.org/wiki/Mobile_Network_Code
> 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 (e.g., en, en-rUS, or en-rGB).
> Screen Size One of small (smaller than HVGA), medium (at least HVGA and smaller than VGA), or large (VGA or larger).
> Screen Width/Length Specify long or notlong for resources designed specifically for wide screen (e.g., WVGA is long, QVGA is notlong).
> Screen Orientation One of port (portrait), land (landscape), or square (square).
> Screen Pixel Density Pixel density in dots per inch (dpi). Best practice is to use ldpi, mdpi, or hdpi to specify low (120 dpi), medium (160 dpi), or high (240 dpi) pixel density respectively. You can specify nodpi for bitmap resources you don't want scaled to support an exact screen density. Unlike with other resource types Android does not require an exact match to select a resource. When selecting the appropriate folder it will choose the nearest match to the device's pixel density and scale the resulting Drawables accordingly.
> Touchscreen Type One of notouch, stylus, or finger.
> Keyboard Availability One of keysexposed, keyshidden, or keyssoft.
> Keyboard Input Type One of nokeys, qwerty, or 12key.
> UI Navigation Type One of nonav, dpad, trackball, or wheel.
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 Drawable resources.
drawable-rUS-en (out of order)
drawable-rUS-rUK (multiple values for a single qualifier)
When Android retrieves a resource at run time, it will find the best match from the available alternatives. Starting with a list of all the folders in which the required value exists, it will select the one with the greatest number of matching qualifiers. If two folders are an equal match, the tiebreaker will be based on the order of the matched qualifiers in the preceding list.
If no resource matches are found on a given device, your application will throw an exception when attempting to access that resource. To avoid this you should always include default values for each resource type in a folder that includes no qualifiers.
Was this article helpful?
Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.