Application Resources >

Providing Resources

Quickview

  • Different types of resources belong in different subdirectories of res/
  • Alternative resources provide configuration-specific resource files

In this document

  1. Grouping Resource Types
  2. Providing Alternative Resources
    1. Qualifier name rules
    2. Creating alias resources
  3. How Android Finds the Best-matching Resource

See also

  1. Accessing Resources
  2. Resource Types
  3. Supporting Multiple Screens

You should always externalize application resources such as images and strings from your code, so that you can maintain them independently. You can also provide alternative resources for specific device configurations, by grouping them in specially-named resource directories. Android will then automatically apply the appropriate resource based on the current configuration. For instance, you might want to provide a different UI layout depending on the screen size.

Once you save your resources external to your application code, you can access them using resource IDs that are generated in your project's R class. How to use resources in your application is discussed in Accessing Resources.

Grouping Resource Types

You should place each type of resource in a specific subdirectory of your project's res/ directory. For example, here's the file hierarchy for a simple project:

MyProject/
    src/  
        MyActivity.java  
    res/
        drawable/  
            icon.png  
        layout/  
            main.xml
            info.xml
        values/  
            strings.xml  

The res/ directory contains all the resources (in subdirectories): an image resource, two layout resources, and a string resource file. The resource directory names are important and are described in table 1.

Table 1. Resource directories supported inside project res/ directory.

Directory Resource Type
anim/ XML files that define tween animations. See Animation Resources.
color/ XML files that define a state list of colors. See Color State List Resource
drawable/

Bitmap files (.png, .9.png, .jpg, .gif) or XML files that are compiled into the following drawable resource subtypes:

  • Bitmap files
  • Nine-Patches (re-sizable bitmaps)
  • State lists
  • Color drawables
  • Shapes
  • Animation drawables

See Drawable Resources.

layout/ XML files that define a user interface layout. See Layout Resource.
menu/ XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu. See Menu Resource.
raw/

Arbitrary files to save in their raw form. Files in here are not compressed by the system. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.

However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead of res/raw/). Files in assets/ are not given a resource ID, so you can read them only using AssetManager.

values/

XML files that contain simple values, such as strings, integers, and colors.

Whereas XML resource files in other res/ subdirectories define a single resource based on the XML filename, files in the values/ directory describe multiple resources. For a file in this directory, each child of the <resources> element defines a single resource. For example, a <string> element creates an R.string resource and a <color> element creates an R.color resource.

Because each resource is defined with its own XML element, you can name the file whatever you want and place different resource types in one file. However, for clarity, you might want to place unique resource types in different files. For example, here are some filename conventions for resources you can create in this directory:

See String Resources, Style Resource, and More Resource Types.

xml/ Arbitrary XML files that can be read at runtime by calling Resources.getXML(). Various XML configuration files must be saved here, such as a searchable configuration.

Note: You should never save resource files directly inside the res/ directory.

For more information about certain types of resources, see the Resource Types documentation.

How to access resources in the res/ subdirectories is discussed in Accessing Resources.

Providing Alternative Resources

Figure 1. Two device configurations, one using alternative resources.

Almost every application should provide alternative resources to support specific device configurations. For instance, you should include alternative drawable resources for different screen densities and alternative string resources for different languages. At runtime, Android automatically detects the current device configuration and loads the appropriate resources.

To specify configuration-specific alternatives for a set of resources:

  1. Create a new directory in res/ named in the form <resources_name>-<config_qualifier>.
    • <resources_name> is the directory name of the corresponding default resources.
    • <config_qualifier> is a name that specifies a configuration for which these resources are to be used.

    You can append more than one <config_qualifier>. Separate each one with a dash.

  2. Save your alternative resources in this new directory. The resource files must be named exactly the same as the default resource files.

For example, here are some default and alternative resources:

res/
    drawable/   
        icon.png
        background.png    
    drawable-hdpi/  
        icon.png
        background.png  

The hdpi qualifier indicates that the resources in that directory are for devices with a high-density screen. While the images in each drawable directory are sized for a specific screen density, the filenames are the same. This way, the resource ID that you use to reference the icon.png or background.png image is always the same, but Android selects the version of that drawable that best matches the current device configuration.

Android supports several configuration qualifiers and you can add multiple qualifiers to one directory name, by separating each qualifier with a dash. Table 2 lists the valid configuration qualifiers, in order of precedence—if you use multiple qualifiers, they must be added to the directory name in the order they are listed in the table.

Table 2. Alternative resource qualifier names.

Qualifier Values Description
MCC and MNC Examples:
mcc310
mcc310-mnc004
mcc208-mnc00
etc.

The mobile country code (MCC), optionally followed by mobile network code (MNC) from the SIM card in the device. For example, mcc310 is U.S. on any carrier, mcc310-mnc004 is U.S. on Verizon, and mcc208-mnc00 is France on Orange.

If the device uses a radio connection (GSM phone), the MCC comes from the SIM, and the MNC comes from the network to which the device is connected.

You can also use the MCC alone (for example, to include country-specific legal resources in your application). If you need to specify based on the language only, then use the language and region qualifier instead (discussed next). If you decide to use the MCC and MNC qualifier, you should do so with care and test that it works as expected.

Also see the configuration fields mcc, and mnc, which indicate the current mobile country code and mobile network code, respectively.

Language and region Examples:
en
fr
en-rUS
fr-rFR
fr-rCA
etc.

The language is defined by a two-letter ISO 639-1 language code, optionally followed by a two letter ISO 3166-1-alpha-2 region code (preceded by lowercase "r").

The codes are not case-sensitive; the r prefix is used to distinguish the region portion. You cannot specify a region alone.

This can change during the life of your application if the user changes his or her language in the system settings. See Handling Runtime Changes for information about how this can affect your application during runtime.

See Localization for a complete guide to localizing your application for other langauges.

Also see the locale configuration field, which indicates the current locale.

Screen size small
normal
large
  • small: Screens based on the space available on a low-density QVGA screen. Considering a portrait HVGA display, this has the same available width but less height—it is 3:4 vs. HVGA's 2:3 aspect ratio. Examples are QVGA low density and VGA high density.
  • normal: Screens based on the traditional medium-density HVGA screen. A screen is considered to be normal if it is at least this size (independent of density) and not larger. Examples of such screens a WQVGA low density, HVGA medium density, WVGA high density.
  • large: Screens based on the space available on a medium-density VGA screen. Such a screen has significantly more available space in both width and height than an HVGA display. Examples are VGA and WVGA medium density screens.

See Supporting Multiple Screens for more information.

Also see the screenLayout configuration field, which indicates whether the screen is small, normal, or large.

Wider/taller screens long
notlong
  • long: Long screens, such as WQVGA, WVGA, FWVGA
  • notlong: Not long screens, such as QVGA, HVGA, and VGA

This is based purely on the aspect ratio of the screen (a "long" screen is wider). This is not related to the screen orientation.

Also see the screenLayout configuration field, which indicates whether the screen is long.

Screen orientation port
land
  • port: Device is in portrait orientation (vertical)
  • land: Device is in landscape orientation (horizontal)

This can change during the life of your application if the user rotates the screen. See Handling Runtime Changes for information about how this affects your application during runtime.

Also see the orientation configuration field, which indicates the current device orientation.

Dock mode car
desk
  • car: Device is in a car dock
  • desk: Device is in a desk dock

Added in API Level 8.

This can change during the life of your application if the user places the device in a dock. You can eneable or disable this mode using UiModeManager. See Handling Runtime Changes for information about how this affects your application during runtime.

Night mode night
notnight
  • night: Night time
  • notnight: Day time

Added in API Level 8.

This can change during the life of your application if night mode is left in auto mode (default), in which case the mode changes based on the time of day. You can eneable or disable this mode using UiModeManager. See Handling Runtime Changes for information about how this affects your application during runtime.

Screen pixel density (dpi) ldpi
mdpi
hdpi
nodpi
  • ldpi: Low-density screens; approximately 120dpi.
  • mdpi: Medium-density (on traditional HVGA) screens; approximately 160dpi.
  • hdpi: High-density screens; approximately 240dpi.
  • nodpi: This can be used for bitmap resources that you do not want to be scaled to match the device density.

There is thus a 4:3 scaling factor between each density, so a 9x9 bitmap in ldpi is 12x12 in mdpi and 16x16 in hdpi.

When Android selects which resource files to use, it handles screen density differently than the other qualifiers. In step 1 of How Android finds the best matching directory (below), screen density is always considered to be a match. In step 4, if the qualifier being considered is screen density, Android selects the best final match at that point, without any need to move on to step 5.

See Supporting Multiple Screens for more information about how to handle screen sizes and how Android might scale your bitmaps.

Touchscreen type notouch
stylus
finger
  • notouch: Device does not have a touchscreen.
  • stylus: Device has a resistive touchscreen that's suited for use with a stylus.
  • finger: Device has a touchscreen.

Also see the touchscreen configuration field, which indicates the type of touchscreen on the device.

Keyboard availability keysexposed
keyssoft
  • keysexposed: Device has a keyboard available. If the device has a software keyboard enabled (which is likely), this may be used even when the hardware keyboard is not exposed to the user, even if the device has no hardware keyboard. If no software keyboard is provided or it's disabled, then this is only used when a hardware keyboard is exposed.
  • keyshidden: Device has a hardware keyboard available but it is hidden and the device does not have a software keyboard enabled.
  • keyssoft: Device has a software keyboard enabled, whether it's visible or not.

If you provide keysexposed resources, but not keyssoft resources, the system uses the keysexposed resources regardless of whether a keyboard is visible, as long as the system has a software keyboard enabled.

This can change during the life of your application if the user opens a hardware keyboard. See Handling Runtime Changes for information about how this affects your application during runtime.

Also see the configuration fields hardKeyboardHidden and keyboardHidden, which indicate the visibility of a hardware keyboard and and the visibility of any kind of keyboard (including software), respectively.

Primary text input method nokeys
qwerty
12key
  • nokeys: Device has no hardware keys for text input.
  • qwert: Device has a hardware qwerty keyboard, whether it's visible to the user or not.
  • 12key: Device has a hardware 12-key keyboard, whether it's visible to the user or not.

Also see the keyboard configuration field, which indicates the primary text input method available.

Navigation key availability navexposed
navhidden
  • navexposed: Navigation keys are available to the user.
  • navhidden: Navigation keys are not available (such as behind a closed lid).

This can change during the life of your application if the user reveals the navigation keys. See Handling Runtime Changes for information about how this affects your application during runtime.

Also see the navigationHidden configuration field, which indicates whether navigation keys are hidden.

Primary non-touch navigation method nonav
dpad
trackball
wheel
  • nonav: Device has no navigation facility other than using the touchscreen.
  • dpad: Device has a directional-pad (d-pad) for navigation.
  • trackball: Device has a trackball for navigation.
  • wheel: Device has a directional wheel(s) for navigation (uncommon).

Also see the navigation configuration field, which indicates the type of navigation method available.

API Level Examples:
v4
v5
v6
v7
etc.

The API Level supported by the device, for example v1 for API Level 1 (Android 1.0) or v5 for API Level 5 (Android 2.0). See the Android API Levels document for more information about these values.

Qualifier name rules

Here are some rules about using resource qualifier names:

  • You can specify multiple qualifiers for a single set of resources, separated by dashes. For example, drawable-en-rUS-land applies to US-English devices in landscape orientation.
  • The qualifiers must be in the order listed in table 2. For example:
    • Wrong: drawable-hdpi-port/
    • Correct: drawable-port-hdpi/
  • Alternative resource directories cannot be nested. For example, you cannot have res/drawable/drawable-en/.
  • Values are case-insensitive. The resource compiler converts directory names to lower case before processing to avoid problems on case-insensitive file systems. Any capitalization in the names is only to benefit readability.
  • Only one value for each qualifier type is supported. For example, if you want to use the same drawable files for Spain and France, you cannot have a directory named drawable-rES-rFR/. Instead you need two resource directories, such as drawable-rES/ and drawable-rFR/, which contain the appropriate files. However, you are not required to actually duplicate the same files in both locations. Instead, you can create an alias to a resource. See Creating alias resources below.

After you save alternative resources into directories named with these qualifiers, Android automatically applies the resources in your application based on the current device configuration. Each time a resource is requested, Android checks for alternative resource directories that contain the requested resource file, then finds the best-matching resource (discussed below).

Creating alias resources

When you have a resource that you'd like to use for more than one device configuration (but not for all configurations), you do not need to put the same resource in each alternative resource directory. Instead, you can (in some cases) create an alternative resource that acts as an alias for a resource saved in your default resource directory.

Note: Not all resources offer a mechanism by which you can create an alias to another resource. In particular, animation, menu, raw, and other unspecified resources in the xml/ directory do not offer this feature.

For example, imagine you have an application icon, icon.png, and need unique version of it for different locales. However, two locales, English-Canadian and French-Canadian, need to use the same version. You might assume that you need to copy the same image into the resource directory for both English-Canadian and French-Canadian, but it's not true. Instead, you can save the image that's used for both as icon_ca.png (any name other than icon.png) and put it in the default res/drawable/ directory. Then create an icon.xml file in res/drawable-en-rCA/ and res/drawable-fr-rCA/ that refers to the icon_ca.png resource using the <bitmap> element. This allows you to store just one version of the PNG file and two small XML files that point to it. (An example XML file is shown below.)

Drawable

To create an alias to an existing drawable, use the <bitmap> element. For example:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/icon_ca" />

If you save this file as icon.xml (in an alternative resource directory, such as res/drawable-en-rCA/), it is compiled into a resource that you can reference as R.drawable.icon, but is actually an alias for the R.drawable.icon_ca resource (which is saved in res/drawable/).

Layout

To create an alias to an existing layout, use the <include> element, wrapped in a <merge>. For example:

<?xml version="1.0" encoding="utf-8"?>
<merge>
    <include layout="@layout/main_ltr"/>
</merge>

If you save this file as main.xml, it is compiled into a resource you can reference as R.layout.main, but is actually an alias for the R.layout.main_ltr resource.

Strings and other simple values

To create an alias to an existing string, simply use the resource ID of the desired string as the value for the new string. For example:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello</string>
    <string name="hi">@string/hello</string>
</resources>

The R.string.hi resource is now an alias for the R.string.hello.

Other simple values work the same way. For example, a color:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="yellow">#f00</color>
    <color name="highlight">@color/red</color>
</resources>

How Android Finds the Best-matching Resource

When you request a resource for which you provide alternatives, Android selects which alternative resource to use at runtime, depending on the current device configuration. To demonstrate how Android selects an alternative resource, assume the following drawable directories each contain different versions of the same images:

drawable/
drawable-en/
drawable-fr-rCA/
drawable-en-port/
drawable-en-notouch-12key/
drawable-port-ldpi/
drawable-port-notouch-12key/

And assume the following is the device configuration:

Locale = en-GB
Screen orientation = port
Screen pixel density = hdpi
Touchscreen type = notouch
Primary text input method = 12key

By comparing the device configuration to the available alternative resources, Android selects drawables from drawable-en-port. It arrives at this decision using the following logic:

Figure 2. Flowchart of how Android finds the best-matching resource.

  1. Eliminate resource files that contradict the device configuration.

    The drawable-fr-rCA/ directory is eliminated, because it contradicts the en-GB locale.

    drawable/
    drawable-en/
    drawable-fr-rCA/
    drawable-en-port/
    drawable-en-notouch-12key/
    drawable-port-ldpi/
    drawable-port-notouch-12key/
    

    Exception: Screen pixel density is the one qualifier that is not eliminated due to a contradiction. Even though the screen density of the device is mdpi, drawable-port-ldpi/ is not eliminated because every screen density is considered to be a match at this point. More information is available in the Supporting Multiple Screens document.

  2. Pick the (next) highest-precedence qualifier in the list (table 2). (Start with MCC, then move down.)
  3. Do any of the resource directories include this qualifier?
    • If No, return to step 2 and look at the next qualifier. (In the example, the answer is "no" until the language qualifier is reached.)
    • If Yes, continue to step 4.
  4. Eliminate resource directories that do not include this qualifier. In the example, the system eliminates all the directories that do not include a language qualifier:
  5. drawable/
    drawable-en/
    drawable-en-port/
    drawable-en-notouch-12key/
    drawable-port-ldpi/
    drawable-port-notouch-12key/
    

    Exception: If the qualifier in question is screen pixel density, Android selects the option that most closely matches the device, and the selection process is complete. In general, Android prefers scaling down a larger original image to scaling up a smaller original image. See Supporting Multiple Screens.

  6. Go back and repeat steps 2, 3, and 4 until only one directory remains. In the example, screen orientation is the next qualifier for which there are any matches. So, resources that do not specify a screen orientation are eliminated:
    drawable-en/
    drawable-en-port/
    drawable-en-notouch-12key/
    

    The remaining directory is drawable-en-port.

Though this procedure is executed for each resource requested, the system further optimizes some aspects. One such optimization is that once the device configuration is known, it might eliminate alternative resources that can never match. For example, if the configuration language is English ("en"), then any resource directory that has a language qualifier set to something other than English is never included in the pool of resources checked (though a resource directory without the language qualifier is still included).

Note: The precedence of the qualifier (in table 2) is more important than the number of qualifiers that exactly match the device. For example, in step 4 above, the last choice on the list includes three qualifiers that exactly match the device (orientation, touchscreen type, and input method), while drawable-en has only one parameter that matches (language). However, language has a higher precedence than these other qualifiers, so drawable-port-notouch-12key is out.

To learn more about how to use resources in your application, continue to Accessing Resources.

↑ Go to top

← Back to Application Resources