본문
App resources overview
Resources are the additional files and static content that your code uses, such as bitmaps, layout definitions, user interface strings, animation instructions, and more.
You should always externalize app resources such as images and strings from your code, so that you can maintain them independently. You should also provide alternative resources for specific device configurations, by grouping them in specially-named resource directories. At runtime, Android uses the appropriate resource based on the current configuration. For example, you might want to provide a different UI layout depending on the screen size or different strings depending on the language setting.
Once you externalize your app resources, you can access them using resource IDs that are generated in your project's R class. This document shows you how to group your resources in your Android project and provide alternative resources for specific device configurations, and then access them from your app code or other XML files.
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/
graphic.png
layout/
main.xml
info.xml
mipmap/
icon.png
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, mipmap/ directories for launcher icons, 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.
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 (.png, .9.png, .jpg, .gif) or XML files that are compiled into the following drawable resource subtypes:
|
mipmap/ | Drawable files for different launcher icon densities. For more information on managing launcher icons with mipmap/ folders, see Put app icons in mipmap directories. |
layout/ | XML files that define a user interface layout. See Layout Resource. |
menu/ | XML files that define app 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 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/ aren't 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:
|
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. |
font/ | Font files with extensions such as .ttf, .otf, or .ttc, or XML files that include a <font-family> element. For more information about fonts as resources, go to Fonts in XML. |
Caution: Never save resource files directly inside the res/ directory—it causes a compiler error.
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 app. 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 app 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 app.

Figure 1. Two different devices, each using different layout resources.
To specify configuration-specific alternatives for a set of resources:
- Create a new directory in res/ named in the form <resources_name>-<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).
- 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.
Caution: When defining an alternative resource, make sure you also define the resource in a default configuration. Otherwise, your app might encounter runtime exceptions when the device changes a configuration. For example, if you add a string to only values-en and not values, your app might encounter a Resource Not Found exception when the user changes the default system language.
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.
Table 2. Configuration qualifier names.
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 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 app). 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 b+en b+en+US b+es+419 |
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. Android 7.0 (API level 24) introduced support for BCP 47 language tags, which you can use to qualify language- and region-specific resources. A language tag is composed from a sequence of one or more subtags, each of which refines or narrows the range of language identified by the overall tag. To use a BCP 47 language tag, concatenate b+ and a two-letter ISO 639-1 language code, optionally followed by additional subtags separated by +. The language tag can change during the life of your app if the users change their language in the system settings. See Handling Runtime Changes for information about how this can affect your app during runtime. See Localization for a complete guide to localizing your app for other languages. Also see the getLocales() method, which provides the defined list of locales. This list includes the primary locale. |
Layout Direction | ldrtl ldltr |
The layout direction of your app. ldrtl means "layout-direction-right-to-left". ldltr means "layout-direction-left-to-right" and is the default implicit value. This can apply to any resource such as layouts, drawables, or values. For example, if you want to provide some specific layout for the Arabic language and some generic layout for any other "right-to-left" language (like Persian or Hebrew) then you would have the following: res/ layout/ main.xml (Default layout) layout-ar/ main.xml (Specific layout for Arabic) layout-ldrtl/ main.xml (Any "right-to-left" language, except for Arabic, because the "ar" language qualifier has a higher precedence) Note: To enable right-to-left layout features for your app, you must set supportsRtl to "true" and set targetSdkVersion to 17 or higher. Added in API level 17. |
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 app's has at least <N> dps of width available for its UI. 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 qualifier to create the layout resources, res/layout-sw600dp/. The system uses these resources only when the smallest dimension of available screen is at least 600dp, regardless of whether the 600dp side is the user-perceived height or width. The smallest width is a fixed screen size characteristic of the device; the device's smallest width doesn't change when the screen's orientation changes. Using smallest width to determine the general screen size is useful because width is often the driving factor in designing a layout. A UI will often scroll vertically, but have fairly hard constraints on the minimum space it needs horizontally. The available width is also the key factor in determining whether to use a one-pane layout for handsets or multi-pane layout for tablets. Thus, you likely care most about what the smallest possible width will be on each device. The smallest width 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 smallest width, the system declares the smallest width to be smaller than the actual screen size, because those are screen pixels not available for your UI. Some values you might use here for common screen sizes:
Added in API level 13. Also see the android:requiresSmallestWidthDp attribute, which declares the minimum smallestWidth with which your app is compatible, and the smallestScreenWidthDp configuration field, which holds the device's smallestWidth value. |
Available width and height | w<N>dp h<N>dp Examples: w720dp w1024dp h720dp h1024dp etc. |
Specifies a minimum available screen width or height, in dp units at which the resource should be used—defined by the <N> value. These configuration values change when the orientation changes between landscape and portrait to match the current actual width and height. This is often useful to determine whether to use a multi-pane layout, because even on a tablet device, you often won't want the same multi-pane layout for portrait orientation as you do for landscape. Thus, you can use these to specify the minimum width and/or height required for the layout, instead of using both the screen size and orientation qualifiers together. When your app provides multiple resource directories with different values for these configurations, the system uses the one closest to (without exceeding) the device's current screen width. Closest to is determined by adding the differences between the actual screen width and the specified width with the difference between the actual screen height and the specified height, with unspecified heights and widths having the value of 0. The values here take into account screen decorations, so if the device has some persistent UI elements on any edge of the display, it uses a value for the width and height that is smaller than the real screen size, accounting for these UI elements and reducing the app's available space. Some vertical screen decorations that aren't 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 apps must be prepared to deal with a somewhat smaller space than they specify. Note:The system chooses the resource that matches both in width and height. Therefor a resource that specifies both will be strongly preferred to one that specifies only one or the other. For example, if the actual screen is w720dp by h1280dp and one resource is qualified with w720dp and another is qualified as w800dp-h1400dp , the latter will be chosen even though the former is an exact match on what it specifies.Added in API level 13. Also see the screenWidthDp and screenHeightDp configuration fields, which hold the current screen width and height. |
Screen size | small normal large xlarge |
Caution: If all your resources use a size qualifier that is larger than the current screen, the system will not use them and your app will crash at runtime (for example, if all layout resources are tagged with the xlarge qualifier, but the device is a normal-size screen). Added in API level 4. See Supporting Multiple Screens for more information. Also see the screenLayout configuration field, which indicates whether the screen is small, normal, or large. |
Screen aspect | long notlong |
This is based purely on the aspect ratio of the screen (a "long" screen is wider). This isn't related to the screen orientation. Also see the screenLayout configuration field, which indicates whether the screen is long. |
Round screen | round notround |
Also see the isScreenRound() configuration method, which indicates whether the screen is round. |
Wide Color Gamut | widecg nowidecg |
Also see the isScreenWideColorGamut() configuration method, which indicates whether the screen has a wide color gamut. |
High Dynamic Range (HDR) | highdr lowdr |
Also see the isScreenHdr() configuration method, which indicates whether the screen has a HDR capabilities. |
Screen orientation | port land |
Also see the orientation configuration field, which indicates the current device orientation. |
UI mode | car desk television appliance watch vrheadset |
This can change during the life of your app if the user places the device in a dock. You can enable or disable some of these modes using UiModeManager. See Handling Runtime Changes for information about how this affects your app during runtime. |
Night mode | night notnight |
This can change during the life of your app 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 UiModeManager. See Handling Runtime Changes for information about how this affects your app during runtime. |
Screen pixel density (dpi) | ldpi mdpi hdpi xhdpi xxhdpi xxxhdpi nodpi tvdpi anydpi nnndpi |
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 doesn't imply that the resources are only for screens of that density. If you don't 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 |
|
Keyboard availability | keysexposed keyshidden keyssoft |
This can change during the life of your app if the user opens a hardware keyboard. See Handling Runtime Changes for information about how this affects your app during runtime. Also see the configuration fields hardKeyboardHidden and keyboardHidden, which indicate the visibility of a hardware keyboard and the visibility of any kind of keyboard (including software), respectively. |
Primary text input method | nokeys qwerty 12key |
|
Navigation key availability | navexposed navhidden |
Also see the navigationHidden configuration field, which indicates whether navigation keys are hidden. |
Primary non-touch navigation method | nonav dpad trackball wheel |
|
Platform Version (API level) | Examples: v3 v4 v7 etc. |
The API level supported by the device. For example, v1 for API level 1 (devices with Android 1.0 or higher) and v4 for API level 4 (devices with Android 1.6 or higher). See the Android API levels document for more information about these values. |
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 automatically includes 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).
단어
1. static : 정적인, 고정된
2. externalize : 외부화하다, 표면화하다
3. hierarchy : 계층
4. distinguish : 구분하다
5. density : 밀도
6. Arbitrary : 임시적인, 원시적인
7. clarity : 명확성
8. high-density : 고화질, 고밀도
9. encounter : 맞닥뜨리다, 부딪히다
10. in order of precedence : 순차적으로, 우선순위대로
11. refine : 개선하다
12. concatenate : 연관시키다, 연결하다
13. axis : 축
14. compatible : 호환될 수 있는
15. portrait : 세로
16. approximately : 거의
17. implicitly : 암묵적으로
정리
1. Almost every app should provide alternative resources to support specific device configurations.
출처
https://developer.android.com/guide/topics/resources/providing-resources
앱 리소스 개요 | Android 개발자 | Android Developers
앱 리소스 개요 리소스는 코드에서 사용하는 추가 파일과 정적인 콘텐츠입니다. 예를 들어 비트맵, 레이아웃 정의, 사용자 인터페이스 문자열, 애니메이션 지침 등이 있습니다. 이미지나 문자열
developer.android.com
'안드로이드 > Studying English with Android' 카테고리의 다른 글
Binary numbers : 2진수 (0) | 2022.03.30 |
---|---|
[DAY 2] 2. Activating components (0) | 2022.02.21 |
[DAY 1] 1. Application Fundamentals (0) | 2022.02.19 |