Providing 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
As you can see in this example, 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.
Directory | Resource Type |
---|---|
animator/
|
XML files that define property animations. |
anim/
|
XML files that define tween animations. (Property animations can also be saved in this directory, but the animator/ directory is preferred for property animations to distinguish between the two types.) |
color/
|
XML files that define a state list of colors. See Color State List Resource |
drawable/
|
Bitmap files (
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. To open these resources with a raw
However, if you need access to original file names and file hierarchy, you might consider saving some resources in the |
values/
|
XML files that contain simple values, such as strings, integers, and colors.
Whereas XML resource files in other 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. |
Caution: Never save resource files directly inside the res/
directory—it will cause a compiler error.
For more information about certain types of resources, see the Resource Types documentation.
The resources that you save in the subdirectories defined in table 1 are your "default" resources. That is, these resources define the default design and content for your application. However, different types of Android-powered devices might call for different types of resources. For example, if a device has a larger than normal screen, then you should provide different layout resources that take advantage of the extra screen space. Or, if a device has a different language setting, then you should provide different string resources that translate the text in your user interface. To provide these different resources for different device configurations, you need to provide alternative resources, in addition to your default resources.
Providing 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 detects the current device configuration and loads the appropriate resources for your application.
To specify configuration-specific alternatives for a set of resources:
-
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 (defined in table 1). -
<qualifier>
is a name that specifies an individual configuration for which these resources are to be used (defined in table 2).
You can append more than one
<qualifier>
. Separate each one with a dash.Caution: When appending multiple qualifiers, you must place them in the same order in which they are listed in table 2. If the qualifiers are ordered wrong, the resources are ignored.
-
- Save the respective 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. The images in each of these drawable directories are sized for a specific screen density, but the filenames are exactly 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 each resource that best matches the current device, by comparing the device configuration information with the qualifiers in the resource directory name.
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 for a resource directory, you must add them to the directory name in the order they are listed in the table.
Configuration | 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, If the device uses a radio connection (GSM phone), the MCC and MNC values come from the SIM card. 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 |
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 "
The codes are not case-sensitive; the 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 languages.
Also see the |
smallestWidth |
sw<N>dp Examples: sw320dp sw600dp sw720dp etc. |
The fundamental size of a screen, as indicated by the shortest dimension of the available screen area. Specifically, the device's smallestWidth is the shortest of the screen's available height and width (you may also think of it as the "smallest possible width" for the screen). You can use this qualifier to ensure that, regardless of the screen's current orientation, your application's has at least
For example, if your layout requires that its smallest dimension of screen area be at least 600 dp at all times, then you can use this qualifer to create the layout resources, The smallestWidth of a device takes into account screen decorations and system UI. For example, if the device has some persistent UI elements on the screen that account for space along the axis of the smallestWidth, the system declares the smallestWidth to be smaller than the actual screen size, because those are screen pixels not available for your UI. Thus, the value you use should be the actual smallest dimension required by your layout (usually, this value is the "smallest width" that your layout supports, regardless of the screen's current orientation). Some values you might use here for common screen sizes:
When your application provides multiple resource directories with different values for the smallestWidth qualifier, the system uses the one closest to (without exceeding) the device's smallestWidth. Added in API level 13.
Also see the For more information about designing for different screens and using this qualifier, see the Supporting Multiple Screens developer guide. |
Available width |
w<N>dp Examples: w720dp w1024dp etc. |
Specifies a minimum available screen width, in When your application provides multiple resource directories with different values for this configuration, the system uses the one closest to (without exceeding) the device's current screen width. The value here takes into account screen decorations, so if the device has some persistent UI elements on the left or right edge of the display, it uses a value for the width that is smaller than the real screen size, accounting for these UI elements and reducing the application's available space. Added in API level 13.
Also see the For more information about designing for different screens and using this qualifier, see the Supporting Multiple Screens developer guide. |
Available height |
h<N>dp Examples: h720dp h1024dp etc. |
Specifies a minimum available screen height, in "dp" units at which the resource should be used—defined by the When your application provides multiple resource directories with different values for this configuration, the system uses the one closest to (without exceeding) the device's current screen height. The value here takes into account screen decorations, so if the device has some persistent UI elements on the top or bottom edge of the display, it uses a value for the height that is smaller than the real screen size, accounting for these UI elements and reducing the application's available space. Screen decorations that are not fixed (such as a phone status bar that can be hidden when full screen) are not accounted for here, nor are window decorations like the title bar or action bar, so applications must be prepared to deal with a somewhat smaller space than they specify. Added in API level 13.
Also see the For more information about designing for different screens and using this qualifier, see the Supporting Multiple Screens developer guide. |
Screen size |
small normal large xlarge
|
Note: Using a size qualifier does not imply that the resources are only for screens of that size. If you do not provide alternative resources with qualifiers that better match the current device configuration, the system may use whichever resources are the best match.
Caution: If all your resources use a size qualifier that is larger than the current screen, the system will not use them and your application will crash at runtime (for example, if all layout resources are tagged with the Added in API level 4. See Supporting Multiple Screens for more information.
Also see the |
Screen aspect |
long notlong
|
Added in API level 4. 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 |
Screen orientation |
port land
|
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 |
UI mode |
car desk television
|
Added in API level 8, television added in API 13.
This can change during the life of your application if the user places the device in a dock. You can enable or disable some of these modes using |
Night mode |
night notnight
|
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 enable or disable this mode using |
Screen pixel density (dpi) |
ldpi mdpi hdpi xhdpi nodpi tvdpi
|
There is a 3:4:6:8 scaling ratio between the four primary densities (ignoring the tvdpi density). So, a 9x9 bitmap in ldpi is 12x12 in mdpi, 18x18 in hdpi and 24x24 in xhdpi. If you decide that your image resources don't look good enough on a television or other certain devices and want to try tvdpi resources, the scaling factor is 1.33*mdpi. For example, a 100px x 100px image for mdpi screens should be 133px x 133px for tvdpi. Note: Using a density qualifier does not imply that the resources are only for screens of that density. If you do not provide alternative resources with qualifiers that better match the current device configuration, the system may use whichever resources are the best match. See Supporting Multiple Screens for more information about how to handle different screen densities and how Android might scale your bitmaps to fit the current density. |
Touchscreen type |
notouch finger
|
Also see the |
Keyboard availability |
keysexposed keyshidden keyssoft
|
If you provide 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 |
Primary text input method |
nokeys qwerty 12key
|
Also see the |
Navigation key availability |
navexposed navhidden
|
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 |
Primary non-touch navigation method |
nonav dpad trackball wheel
|
Also see the |
Platform Version (API level) |
Examples:v3 v4 v7 etc. |
The API level supported by the device. For example, Caution: Android 1.5 and 1.6 only match resources with this qualifier when it exactly matches the platform version. See the section below about Known Issues for more information. |
Note: Some configuration qualifiers have been added since Android 1.0, so not all versions of Android support all the qualifiers. Using a new qualifier implicitly adds the platform version qualifier so that older devices are sure to ignore it. For example, using a w600dp
qualifier will automatically include the v13
qualifier, because the available-width qualifier was new in API level 13. To avoid any issues, always include a set of default resources (a set of resources with no qualifiers). For more information, see the section about Providing the Best Device Compatibility with Resources.