본문 바로가기

안드로이드/Studying English with Android

[DAY 3] 3. App resources overview

728x90
반응형

본문

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.

DirectoryResource 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 (.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
  • Shapes
  • Animation drawables
  • Other drawables
See Drawable Resources.
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:
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.
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:

  1. 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).
    You can append more than one <qualifier>. Separate each one with a dash.
  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.
  3. 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.

ConfigurationQualifier ValuesDescription
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:
  • 320, for devices with screen configurations such as:
    • 240x320 ldpi (QVGA handset)
    • 320x480 mdpi (handset)
    • 480x800 hdpi (high-density handset)
  • 480, for screens such as 480x800 mdpi (tablet/handset).
  • 600, for screens such as 600x1024 mdpi (7" tablet).
  • 720, for screens such as 720x1280 mdpi (10" tablet).
When your app 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 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
  • small: Screens that are of similar size to a low-density QVGA screen. The minimum layout size for a small screen is approximately 320x426 dp units. Examples are QVGA low-density and VGA high density.
  • normal: Screens that are of similar size to a medium-density HVGA screen. The minimum layout size for a normal screen is approximately 320x470 dp units. Examples of such screens a WQVGA low-density, HVGA medium-density, WVGA high-density.
  • large: Screens that are of similar size to a medium-density VGA screen. The minimum layout size for a large screen is approximately 480x640 dp units. Examples are VGA and WVGA medium-density screens.
  • xlarge: Screens that are considerably larger than the traditional medium-density HVGA screen. The minimum layout size for an xlarge screen is approximately 720x960 dp units. In most cases, devices with extra-large screens would be too large to carry in a pocket and would most likely be tablet-style devices. Added in API level 9.
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 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
  • long: Long screens, such as WQVGA, WVGA, FWVGA
  • notlong: Not long screens, such as QVGA, HVGA, and VGA
Added in API level 4.
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
  • round: Round screens, such as a round wearable device
  • notround: Rectangular screens, such as phones or tablets
Added in API level 23.
Also see the isScreenRound() configuration method, which indicates whether the screen is round.
Wide Color Gamut widecg
nowidecg
  • widecg: Displays with a wide color gamut such as Display P3 or AdobeRGB
  • nowidecg: Displays with a narrow color gamut such as sRGB
Added in API level 26.
Also see the isScreenWideColorGamut() configuration method, which indicates whether the screen has a wide color gamut.
High Dynamic Range (HDR) highdr
lowdr
  • highdr: Displays with a high-dynamic range
  • lowdr: Displays with a low/standard dynamic range
Added in API level 26.
Also see the isScreenHdr() configuration method, which indicates whether the screen has a HDR capabilities.
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 app if the user rotates the screen. See Handling Runtime Changes for information about how this affects your app during runtime.
Also see the orientation configuration field, which indicates the current device orientation.
UI mode car
desk
television
appliance
watch
vrheadset
  • car: Device is displaying in a car dock
  • desk: Device is displaying in a desk dock
  • television: Device is displaying on a television, providing a "ten foot" experience where its UI is on a large screen that the user is far away from, primarily oriented around DPAD or other non-pointer interaction
  • appliance: Device is serving as an appliance, with no display
  • watch: Device has a display and is worn on the wrist
  • vrheadset: Device is displaying in a virtual reality headset
Added in API level 8, television added in API 13, watch added in API 20.

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
  • night: Night time
  • notnight: Day time
Added in API level 8.
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
  • ldpi: Low-density screens; approximately 120dpi.
  • mdpi: Medium-density (on traditional HVGA) screens; approximately 160dpi.
  • hdpi: High-density screens; approximately 240dpi.
  • xhdpi: Extra-high-density screens; approximately 320dpi. Added in API Level 8
  • xxhdpi: Extra-extra-high-density screens; approximately 480dpi. Added in API Level 16
  • xxxhdpi: Extra-extra-extra-high-density uses (launcher icon only, see the note in Supporting Multiple Screens); approximately 640dpi. Added in API Level 18
  • nodpi: This can be used for bitmap resources that you don't want to be scaled to match the device density.
  • tvdpi: Screens somewhere between mdpi and hdpi; approximately 213dpi. This isn't considered a "primary" density group. It is mostly intended for televisions and most apps shouldn't need it—providing mdpi and hdpi resources is sufficient for most apps and the system scales them as appropriate. Added in API Level 13
  • anydpi: This qualifier matches all screen densities and takes precedence over other qualifiers. This is useful for vector drawables. Added in API Level 21
  • nnndpi: Used to represent non-standard densities, where nnn is a positive integer screen density. This shouldn't be used in most cases. Use standard density buckets, which greatly reduces the overhead of supporting the various device screen densities on the market.
There is a 3:4:6:8:12:16 scaling ratio between the six primary densities (ignoring the tvdpi density). So, a 9x9 bitmap in ldpi is 12x12 in mdpi, 18x18 in hdpi, 24x24 in xhdpi and so on.
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
  • notouch: Device doesn't have a touchscreen.
  • finger: Device has a touchscreen that is intended to be used through direction interaction of the user's finger.
Also see the touchscreen configuration field, which indicates the type of touchscreen on the device.
Keyboard availability keysexposed
keyshidden
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 isn't 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 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
  • nokeys: Device has no hardware keys for text input.
  • qwerty: 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.
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

 

728x90
반응형

'안드로이드 > 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