SlideShare a Scribd company logo
Resources
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
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
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
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
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
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
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
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
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
Resources
<ViewGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@[+][package:]id/resource_name"
android:layout_height=["dimension"|"fill_parent"|"wrap_content"]
android:layout_width=["dimension"|"fill_parent"|"wrap_content"]
[ViewGroup-specific attributes] >
<View
android:id="@[+][package:]id/resource_name"
android:layout_height=["dimension"|"fill_parent"|"wrap_content"]
android:layout_width=["dimension"|"fill_parent"|"wrap_content"]
[View-specific attributes] >
<requestFocus/>
</View>
<ViewGroup >
<View />
</ViewGroup>
<include layout="@layout/layout_resource"/>
</ViewGroup>
Layout
Resources
<resources>
<string
name="string_name"
>text_string</string>
</resources>
String
Resources
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@[package:]anim/interpolator_resource"
android:shareInterpolator=["true" | "false"] >
<alpha
android:fromAlpha="float"
android:toAlpha="float" />
<scale
android:fromXScale="float"
android:toXScale="float"
android:fromYScale="float"
android:toYScale="float"
android:pivotX="float"
android:pivotY="float" />
<translate
android:fromXDelta="float"
android:toXDelta="float"
android:fromYDelta="float"
android:toYDelta="float" />
<rotate
android:fromDegrees="float"
android:toDegrees="float"
android:pivotX="float"
android:pivotY="float" />
</set>
View Animation
Resources
<set
android:ordering=["together" | "sequentially"]>
<objectAnimator
android:propertyName="string"
android:duration="int"
android:valueFrom="float | int | color"
android:valueTo="float | int | color"
android:startOffset="int"
android:repeatCount="int"
android:repeatMode=["repeat" | "reverse"]
android:valueType=["intType" | "floatType"]/>
<animator
android:duration="int"
android:valueFrom="float | int | color"
android:valueTo="float | int | color"
android:startOffset="int"
android:repeatCount="int"
android:repeatMode=["repeat" | "reverse"]
android:valueType=["intType" | "floatType"]/>
</set>
Property Animation
Resources
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@[+][package:]id/resource_name"
android:title="string"
android:titleCondensed="string"
android:icon="@[package:]drawable/drawable_resource_name"
android:onClick="method name"
android:showAsAction=["ifRoom" | "never" | "withText" | "always" | "collapseActionView"]
android:actionLayout="@[package:]layout/layout_resource_name"
android:actionViewClass="class name"
android:actionProviderClass="class name"
android:alphabeticShortcut="string"
android:numericShortcut="string"
android:checkable=["true" | "false"]
android:visible=["true" | "false"]
android:enabled=["true" | "false"]
android:menuCategory=["container" | "system" | "secondary" | "alternative"]
android:orderInCategory="integer" />
<group android:id="@[+][package:]id/resource name"
android:checkableBehavior=["none" | "all" | "single"]
android:visible=["true" | "false"]
android:enabled=["true" | "false"]
android:menuCategory=["container" | "system" | "secondary" | "alternative"]
android:orderInCategory="integer" >
<item />
</group>
<item >
<menu>
<item />
</menu>
</item>
</menu>
Menu
Resources
<resources>
<style
name="style_name"
parent="@[package:]style/style_to_inherit">
<item
name="[package:]style_property_name">style_value</item>
</style>
</resources>
Style
Resources
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize=["true" | "false"]
android:dither=["true" | "false"]
android:variablePadding=["true" | "false"] >
<item
android:drawable="@[package:]drawable/drawable_resource"
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_hovered=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_activated=["true" | "false"]
android:state_window_focused=["true" | "false"] />
</selector>
Drawable Selector
Resources
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"] >
<corners
android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
<gradient
android:angle="integer"
android:centerX="integer"
android:centerY="integer"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
android:useLevel=["true" | "false"] />
<padding
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />
<size
android:width="integer"
android:height="integer" />
<solid
android:color="color" />
<stroke
android:width="integer"
android:color="color"
android:dashWidth="integer"
android:dashGap="integer" />
</shape>
Shape
Resources
<resources>
<bool
name="bool_name"
>[true | false]</bool>
<color
name="color_name"
>hex_color</color>
<dimen
name="dimension_name"
>dimension</dimen>
<integer
name="integer_name"
>integer</integer>
<integer-array
name="integer_array_name">
<item>integer</item>
</integer-array>
<array
name="integer_array_name">
<item>resource</item>
</array>
</resources>
Other Resources

More Related Content

ODP
Android App Development - 01 Introduction
Diego Grancini
 
PDF
Keep calm and write reusable code in Android
Juan Camilo Sacanamboy
 
KEY
Android momobxl
Steven Palmaers
 
PDF
Android Development - Process & Tools
Lope Emano
 
ODP
Moodle Development Best Pracitces
Justin Filip
 
PDF
How to Build Developer Tools on Top of IntelliJ Platform
intelliyole
 
ODP
Android App Development - 14 location, media and notifications
Diego Grancini
 
ODP
Android App Development - 07 Threading
Diego Grancini
 
Android App Development - 01 Introduction
Diego Grancini
 
Keep calm and write reusable code in Android
Juan Camilo Sacanamboy
 
Android momobxl
Steven Palmaers
 
Android Development - Process & Tools
Lope Emano
 
Moodle Development Best Pracitces
Justin Filip
 
How to Build Developer Tools on Top of IntelliJ Platform
intelliyole
 
Android App Development - 14 location, media and notifications
Diego Grancini
 
Android App Development - 07 Threading
Diego Grancini
 

Similar to Android App Development - 03 Resources (20)

PDF
Android App Development 08 : Support Multiple Devices
Anuchit Chalothorn
 
PDF
Android resource
Krazy Koder
 
PDF
Chapter 9 - Resources System
Sittiphol Phanvilai
 
PPTX
03 android application structure
Sokngim Sa
 
PPT
Android project architecture
Sourabh Sahu
 
DOCX
Android Resources.docx
KNANTHINIMCA
 
PPT
Unit 2 in environment science and technology
saimohith981
 
PPTX
Assets, files, and data parsing
Aly Arman
 
PDF
Android Development
mclougm4
 
PPTX
Android structure
Kumar
 
PPTX
Globalization and accessibility
aspnet123
 
PDF
Android resources
ma-polimi
 
PPT
Synapseindia android apps introduction hello world
Tarunsingh198
 
PDF
Android dev tips
Kanda Runapongsa Saikaew
 
DOCX
lec1.docx
ismailaboshatra
 
DOCX
Android style resource and other resources types-chapter12
Dr. Ramkumar Lakshminarayanan
 
PDF
Intro to-puppet
F.L. Jonathan Araña Cruz
 
PDF
Android stepbystep
Krazy Koder
 
PDF
Devtools cheatsheet
Dr. Volkan OBAN
 
PDF
Devtools cheatsheet
Dieudonne Nahigombeye
 
Android App Development 08 : Support Multiple Devices
Anuchit Chalothorn
 
Android resource
Krazy Koder
 
Chapter 9 - Resources System
Sittiphol Phanvilai
 
03 android application structure
Sokngim Sa
 
Android project architecture
Sourabh Sahu
 
Android Resources.docx
KNANTHINIMCA
 
Unit 2 in environment science and technology
saimohith981
 
Assets, files, and data parsing
Aly Arman
 
Android Development
mclougm4
 
Android structure
Kumar
 
Globalization and accessibility
aspnet123
 
Android resources
ma-polimi
 
Synapseindia android apps introduction hello world
Tarunsingh198
 
Android dev tips
Kanda Runapongsa Saikaew
 
lec1.docx
ismailaboshatra
 
Android style resource and other resources types-chapter12
Dr. Ramkumar Lakshminarayanan
 
Intro to-puppet
F.L. Jonathan Araña Cruz
 
Android stepbystep
Krazy Koder
 
Devtools cheatsheet
Dr. Volkan OBAN
 
Devtools cheatsheet
Dieudonne Nahigombeye
 
Ad

More from Diego Grancini (10)

ODP
Android App Development - 13 Broadcast receivers and app widgets
Diego Grancini
 
ODP
Android App Development - 12 animations
Diego Grancini
 
ODP
Android App Development - 11 Lists, grids, adapters, dialogs and toasts
Diego Grancini
 
ODP
Android App Development - 10 Content providers
Diego Grancini
 
ODP
Android App Development - 09 Storage
Diego Grancini
 
ODP
Android App Development - 08 Services
Diego Grancini
 
ODP
Android App Development - 06 Fragments
Diego Grancini
 
ODP
Android App Development - 05 Action bar
Diego Grancini
 
ODP
Android App Development - 04 Views and layouts
Diego Grancini
 
ODP
Android App Development - 02 Activity and intent
Diego Grancini
 
Android App Development - 13 Broadcast receivers and app widgets
Diego Grancini
 
Android App Development - 12 animations
Diego Grancini
 
Android App Development - 11 Lists, grids, adapters, dialogs and toasts
Diego Grancini
 
Android App Development - 10 Content providers
Diego Grancini
 
Android App Development - 09 Storage
Diego Grancini
 
Android App Development - 08 Services
Diego Grancini
 
Android App Development - 06 Fragments
Diego Grancini
 
Android App Development - 05 Action bar
Diego Grancini
 
Android App Development - 04 Views and layouts
Diego Grancini
 
Android App Development - 02 Activity and intent
Diego Grancini
 
Ad

Android App Development - 03 Resources