Resources are the additional files and static content that your code uses, such as bitmaps, layout definitions, user interface strings, animation instructions, and more. Show
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 Grouping resource typesYou should place each type of resource in a specific subdirectory of your project's 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 Note: For more information about using the mipmap folders, see Put app icons in mipmap directories. Table 1. Resource directories supported inside project
Caution: Never save resource files directly inside the 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 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 resourcesAlmost 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:
For example, here are some default and alternative resources: res/ drawable/ icon.png background.png drawable-hdpi/ icon.png background.png The 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 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.
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 Qualifier name rulesHere are some rules about using configuration qualifier names:
After you save alternative resources into directories named with these qualifiers, Android automatically applies the resources in your app 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). If there are no alternative resources that match a particular device configuration, then Android uses the corresponding default resources (the set of resources for a particular resource type that doesn't include a configuration qualifier). Creating alias resourcesWhen you have a resource that you'd like to use for more than one device configuration (but don't want to provide as a default resource), you don't need to put the same resource in more than one 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 For example, imagine you have an app icon, DrawableTo create an alias to an existing drawable, use the <?xml version="1.0" encoding="utf-8"?> <resources> <drawable name="icon">@drawable/icon_ca</drawable> </resources> If you save this file as LayoutTo create an alias to an existing layout, use the <?xml version="1.0" encoding="utf-8"?> <merge> <include layout="@layout/main_ltr"/> </merge> If you save this file as Strings and other simple valuesTo 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 Other simple values work the same way. For example, a color: <?xml version="1.0" encoding="utf-8"?> <resources> <color name="red">#f00</color> <color name="highlight">@color/red</color> </resources> Accessing your app resourcesOnce you provide a resource in your application, you can apply it by referencing its resource ID. All resource IDs are defined in your project's When your application is compiled, Although the
There are two ways you can access a resource:
Accessing resources in codeYou can use a resource in code by passing the resource ID as a method parameter. For example, you can set an
You can also retrieve individual resources using methods in SyntaxHere's the syntax to reference a resource in code: [<package_name>.]R.<resource_type>.<resource_name>
See Resource Types for more information about each resource type and how to reference them. Use casesThere are many methods that
accept a resource ID parameter and you can retrieve resources using methods in Here are some examples of accessing resources in code:
Caution: You should never modify the Accessing resources from XMLYou can define values for some XML attributes and elements using a reference to an existing resource. You will often do this when creating layout files, to supply strings and images for your widgets. For example, if you add a <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/submit" /> SyntaxHere is the syntax to reference a resource in an XML resource: @[<package_name>:]<resource_type>/<resource_name>
See Resource Types for more information about each resource type and how to reference them. Use casesIn some cases you must use a resource for a value in XML (for example, to apply a drawable image to a widget), but you can also use a resource in XML any place that accepts a simple value. For example, if you have the following resource file that includes a color resource and a string resource: <?xml version="1.0" encoding="utf-8"?> <resources> <color name="opaque_red">#f00</color> <string name="hello">Hello!</string> </resources> You can use these resources in the following layout file to set the text color and text string: <?xml version="1.0" encoding="utf-8"?> <EditText xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@color/opaque_red" android:text="@string/hello" /> In this case you don't need to specify the package name in the resource reference because the resources are from your own package. To reference a system resource, you would need to include the package name. For example: <?xml version="1.0" encoding="utf-8"?> <EditText xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@android:color/secondary_text_dark" android:text="@string/hello" /> Note: You should use string resources at all times, so that your application can be localized for other languages. For information about creating alternative resources (such as localized strings), see Providing alternative resources. For a complete guide to localizing your application for other languages, see Localization. You can even use resources in XML to create aliases. For example, you can create a drawable resource that is an alias for another drawable resource: <?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/other_drawable" /> This sounds redundant, but can be very useful when using alternative resource. Read more about Creating alias resources. Referencing style attributesA style attribute resource allows you to reference the value of an attribute in the currently-applied theme. Referencing a style attribute allows you to customize the look of UI elements by styling them to match standard variations supplied by the current theme, instead of supplying a hard-coded value. Referencing a style attribute essentially says, "use the style that is defined by this attribute, in the current theme." To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol ( ?[<package_name>:][<resource_type>/]<resource_name> For example, here's how you can reference an attribute to set the text color to match the "secondary" text color of the system theme: <EditText id="text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="?android:textColorSecondary" android:text="@string/hello_world" /> Here, the Accessing original filesWhile uncommon, you might need access your original files and directories. If you do, then saving your files in Files saved in the
However, if all you require is the ability to read raw data (such as a video or audio file), then save the file in the Accessing platform resourcesAndroid contains a number of standard resources, such as styles, themes, and layouts. To access these resource, qualify your resource reference with the
In this example, Providing the best device compatibility with resourcesIn order for your app to support multiple device configurations, it's very important that you always provide default resources for each type of resource that your app uses. For example, if your app
supports several languages, always include a Likewise, if you provide different layout resources based on the screen orientation, you should pick one orientation as your default. For example, instead of providing layout resources in Providing default resources is
important not only because your app might run on a configuration you hadn't anticipated, but also because new versions of Android sometimes add configuration qualifiers that older versions don't support. If you use a new resource qualifier, but maintain code compatibility with older versions of Android, then when an older version of Android runs your app, it will crash if you don't provide default resources, because it cannot use the resources named with the new qualifier. For example, if your
So, in order to provide the best device compatibility, always provide default resources for the resources your app needs to perform properly. Then create alternative resources for specific device configurations using the configuration qualifiers. There is one exception to this rule: If your app's
How Android finds the best-matching resourceWhen 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 = By comparing the device configuration to the available alternative resources, Android selects drawables from The system arrives at its decision for which resources to use with the following logic: Figure 2. Flowchart of how Android finds the best-matching resource.
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). When selecting resources based on the screen size qualifiers, the system uses resources designed for a screen smaller than the current screen if there are no
resources that better match (for example, a large-size screen uses normal-size screen resources if necessary). However, if the only available resources are larger than the current screen, the system doesn't use them and your app will crash if no other resources match the device configuration (for example, if all layout resources are tagged with the 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 What are logical data structures that detail the relationships among data elements using graphics or images multiple choice?Cards
What is a collection of related data elements?A database is an organized collection of interrelated data that serves a number of applications in an enterprise.
What is a collection of related data elements multiple choice question?A collection of related data is called Database. A database is a data structure that stores organized information. Most databases contain multiple tables, which may each include several different fields.
What is a logical data structure that details the relationship among data elements?A logical data model establishes the structure of data elements and the relationships among them. It is independent of the physical database that details how the data will be implemented. The logical data model serves as a blueprint for used data.
|