2. Resources
To enable support for different configurations of the device, an
application uses resources in the /res folder of the project.
The folder contains default and alternative resources (for different
configurations).
The default resources and all the alternatives must be identified
by the same name.
The compiler creates a /gen folder that creates an enumeration
with all the resources available to the application.
The default resources must always be present because they are
loaded at runtime when no other resource is present.
Resources
3. Resources
● Animator: it contains .xml files with Property Animations
● Anim: it contains .xml files with View Animations
● Color: it contains .xml files with Color Selector
● Drawable: it can contain .png or .xml files
● Layout: it contains .xml files with layouts
● Menu: it contains .xml files with Options menus, Context
menus and/or Sub menus
● Raw: it contains all other files to recover during runtime
● Values: it contains .xml files with values for differnt
configurations. They are: strings, arrays, dimens, colors, styles
● Xml: it contains generic .xml files
Types
4. Resources
You need to create the /res subfolder that specify the name of the
type of configuration that supports to provide alternative
resources to support different configurations:
<resources_name>-<config_qualifier>
<resources_name> is the type of resource that contains as
indicated above and <config_qualifier> identifies the supported
configuration.
Ex. res/layout-land contains the layout to be used when the
phone is in landscape mode.
You can specify multiple qualifiers in series.
Alternative Resources
5. Resources
The most commonly used types of qualifiers are listed in the
same order in which they must be specified in the name of the
/res subfolder:
● Language (Ex. en, fr, it,...): it specifies in which language
resources are available within the folder
● SmallestWidth (sw<n>dp Es. sw320dp, sw720dp,...): the
resources of these folders are available only if the terminal has
Min(dp_height, dp_width) >= n
● Screen Size (small, normal, large, xlarge):
Qualifiers - Typologies
6. Resources
● ScreenOrientation (port, land)
● Screen Pixel Density (ldpi, mdpi, hdpi, xhdpi, xxhdpi,
xxxhdpi, tvdpi)
● ldpi: 120dp
● mdpi: 160dp
● hdpi: 240dp
● xhdpi: 320dp
● xxhdpi: 480dpi
● xxxhdpi: 640dp
● Platform Version (Es. V12, v15, v19, ...): the logic is top
bottom. Ex. A device with V19 verifies the presence of the
resource in v19, then in v18, and so on until it finds one.
Qualifiers - Tipologie
7. Resources
● The multiple qualifiers must be specified in the order indicated
before: Es.
res/drawable-port-hdpi/
● The folders defined with these rules can not contain subfolders
● The folder names are case-insensitive
● It is allowed only one value for each type of qualifier. Es.
res/drawable/it-en INCORRECT
If you want to provide the same resource configuration for it and
en, you have to create 2 folders and enter the same resource in
both
res/drawable-it CORRECT
res/drawable-en CORRECT
Qualifiers - Rules
8. Resources
When you insert a new resource in the project, the aapt
automatically generates the R class that contains all the IDs of
resources on the basis of fundamental subdirectory in which you
can retrieve them at runtime:
● R.anim.<anim_ID>
● R.array.<array_ID>
● R.bool.<bool_ID>
● R.color.<color_ID>
● R.dimen.<dimen_ID>
● R.drawable.<drawable_ID>
● R.integer.<integer_ID>
● R.layout.<layout_ID>
● R.menu.<menu_ID>
● R.raw.<raw_ID>
● R.string.<string_ID>
● R.style.<style_ID>
Resource access at runtime
9. Resources
The R class is generated for each project and it is created in the
package defined in the manifest:
[<package>.]R.<resource_type>.<resource_ID>
<package> is optional if the class is imported into the R class is
the same from which you want to retrieve a resource.
In this way it is possible to access resources outside projects and
imported to the system resources of Android for which
<package> = android
Then the developer may require a system resource with the
expression:
android.R.<resource_type>.<resource_ID>
N.B.: system resources at runtime are taken from the firmware of
the terminal, so the same ID can refer to different resources in
terminals with different firmware.
Resource access at runtime
10. Resources
To access resources from an xml file using the following syntax:
@[<package_name>:]<resource_type>/<resource_name>
<package_name> is optional if you want to use a resource
package of the project.
Pattern: during the creation of the layout you should always
include references to external resources instead of entering the
value in the layout.
Es.
android:text="@string/hello" CORRECT
android:text="Hello!" CORRECT BUT NOT
RECOMMENDED
Resource access from an xml file