string

COMPS413F
Supplementary Notes
#01. Resource Management
COMPS413F Supp
#01
1
Agenda
Introduction
Accessing resources


a. Accessing resources in Java code
b. Accessing resources in XML
Types of resources


System resources/Android platform resources
Application resources
System resources/Android platform resources
Application resources

a. Simple resource values




b. Drawable resources



Strings
Colors
Dimensions
Images
c. Layouts
d. Files


XML files
Raw files
Default and alternative resources
COMPS413F Supp
#01
2
Introduction
User interfaces are always generated with the use of different
resources, e.g.,




Strings
Graphics
Multimedia files (e.g., image, audio, or video files)
…
Project resource hierarchy are well-defined for helping
developers to include different types of resources in Android
project


Different types of resources are placed in various subdirectories
under the "res/" directory of the project
E.g.,
 Strings are defined in the file "res/values/strings.xml"
 Layout resources are defined in the directory "res/layout/"
 Image resources are stored in the directory "res/drawable/"
COMPS413F Supp
#01
3
Introduction
The following table depicts the directories under the "res/"
directory of an Android project and the corresponding
resources stored in them
Resource directory
animator/
anim/
color/
drawable/
layout/
menu/
raw/
values/
xml/
COMPS413F Supp
#01
Description of resources
XML files of property animation definitions
XML files of tween animation definitions
XML files of color state list definitions
Image files (e.g., PNG, JPG, GIF) or XML files of drawable definitions
(e.g., shapes)
XML files of user interface layout definitions
XML files of application menu definitions
Any raw files
XML files of value definitions, such as the array tag for arrays, the stringarray tag for string arrays, the color tag for colors, the dimen tag for
dimensions, the string tag for strings, the style tag for styles, etc.
Any XML files
4
Introduction
Two types of resources

System resources/Android platform resources
 Resource included with the system and defined under the
class "android.R", e.g.,



System string resource (android.R.string)
Color resource (android.R.color)
Fade-in and fade-out animations (android.R.anim)
 http://developer.android.com/reference/android/R.html

Application resources
 Created and defined by the developer
 Stored in files within an Android project under the "/res"
directory
 Specific to the application
 E.g., user-defined string resource, layout, image, …
COMPS413F Supp
#01
5
Introduction
Advantage of grouping and compiling
application resources into application package

Program readability is increased as program codes
are more cleaner and easier to read
 => Fewer bugs


More convenient to maintain externalized
resources, which are independent of the program
code
Enables the provision of multiple versions of
resources for different device configurations, such
as different languages or screen sizes
 => Localization and internationalization are more easier
COMPS413F Supp
#01
6
Accessing Resources
For effectively use of resources during Android application
development, they can be referenced in two ways


a. Access resources programmatically (in Java code)
b. Access resources from other compiled resources (in XML)
Resource ID is divided into two parts

i. Resource type
 E.g.,



Directory names

"drawable", "layout", …
Subtypes of values

"string", "bool", and "dimen", …, etc.
ii. Resource name
 E.g.,



COMPS413F Supp
#01
The filenames (excluding file extensions) for drawable and layout resources
The value of the XML "android:name" attribute for values
The value of the XML "android:id" attribute for component IDs
7
Accessing Resources
a. Access resource programmatically (in Java code)


Use the constants defined in the R class
The general syntax is:
 [package.]R.type.name



The package of R (optional)
The resource type
The resource name
b. Accessing resources from other compiled resources (in
XML)

Use the special XML syntax:
 @[package:]type/name



COMPS413F Supp
#01
The package of the resource (optional)
The resource type
The resource name
8
Accessing Resources
The mechanism of using resources in Java code and XML
@[package:] Resource
IDs are
type/name
referenced
in XML files.
XML file
Resource
XML data contains
resource content.
E.g.,

R.java
Constants in [package.]
R.java are R.type.name
used in Java
programs.
Java
program
Programs use
resources at runtime.
Assume the file "res/values/strings.xml" contains a piece of resource with type "string" and
name "app_name" as follows:


<string name="app_name">FirstApp</string>
a. The above string resource is referenced in Java code

b. The above string resource is referenced in in XML


Note:

aapt
generates
R.java from
resources.
R.string.app_name
@string/app_name
Since filenames without extension are used as identification names for some resources, they
must be unique for the same resource type

E.g., two images with the same file name may NOT be included as drawable resources in an application
even they have different extension

COMPS413F Supp
#01
E.g., "pc.png" and "pc.jpg"
9
System Resources/Android
Platform Resources
The Android platform provides built-in resources that any applications may use
These platform resources are defined in the inner classes of the "android.R" class
E.g., system resources string are implemented as "android.R.string" class and there
are some commonly used string already defined that can be used by developers
directly

E.g.,

E.g., "OK", "Cancel", "Yes", "No", …
a. Reference a system resource string with program code



1. Call the static method "getSystem" of the Resources class to retrieve the global system "Resource"
object
2. Call the "getString()" method with an appropriate string resource name
E.g., referencing system resource string "Yes"
String yesString = Resources.getSystem().getString(android.R.string.yes);

b. Reference a system resource string from other compiled resource, e.g., in a layout file

E.g., attribute for referencing system resource string "Yes"
@android:string/yes
Note:

Always use system resources as much as possible to keep the application small and efficient
COMPS413F Supp
#01
10
Application Resources
Different kind of application are specified and developed
by integrating different types of application resources
which are supported by the Android platform
Common application resource types:

a. Simple resource values
 E.g., strings, colors, dimensions

b. Drawable graphics files
 E.g., images, 2D/3D graphics, and icons, …

c. Layout files
 Layout configuration of different screen
 E.g., menu screen, configuration screen, and help screen

d. Raw files of all types
 E.g., XML file, sound file, help file, …
COMPS413F Supp
#01
11
Application Resources
Different resource types are defined with special XML tags and stored into specially
named directory

Some subdirectories under "/res" are created by default when a new Android project is
created, e.g.,



"/drawable" for graphics files
"/layout" for layout configuration
"/value" for string value
Rules of defining resource filenames under the "/res" subfolder



E.g.,

Resource filenames must be lowercase
Resource filenames can only contain letters (‘a’-‘z’), numbers (0-9), underscores (‘_’), and
periods (‘.’)
Resource filenames must be unique
i. Reference a string in program code by using the generated class R (R.java)



1. Call the "getResource" method to retrieve "Resource" object
2. Call the appropriate method with respect to the type of resource which is going to be retrieved
E.g., referencing a string named "aString" which is defined in the resource file "strings.xml"
String resultString = getResources().getString(R.string.aString);

ii. Reference a string from other compiled resource, e.g., in a layout file

E.g., attribute for referencing string resource "aString"
COMPS413F Supp
#01
@string/aString
12
a. Simple resource values
Simple resources should be defined in XML files

The XML files should be put into the "/res/values" folder
within the project directory
 "name/value" pairs are represented by using special XML tags

These types of resources are compiled into the application
package (.apk) during build time
Simple resources can then be managed by editing the
XML files directly
Example of simple resources



1. Strings
2. Colors
3. Dimensions
COMPS413F Supp
#01
13
1. String
Used for displaying textual information
Should be stored in file "/res/values/strings.xml"

Note:

The file is generated automatically when the project is created
Tagged by "<string>" tag
E.g.,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">App Name</string>
<string name="aString">A string</string>
<string name="complex_string">Resource strings may contain
\"double quotes\", \'single quotes\',
<b>bold text</b>, <i>italic text</i>, etc.</string>
</resources>

The above "strings.xml" file defines three string resources
Accessing string resource programmatically or in other compiled resources


String resultString = getResources().getString(R.string.aString);
@string/complex_string
COMPS413F Supp
#01
14
2. Colors
Used for screen control
Should be stored in file "/res/values/colors.xml"

Note:

The file should be created manually as it will not be created by default
Tagged by "<color>" tag
E.g.,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="background_color">#006400</color>
<color name="app_text_color">#FFE4C4</color>
</resources>
Accessing color resource programmatically
int bgColor = getResources().getColor(R.color.app_text_color);
TextView textView = (TextView) findViewById(R.id.aTextView);
textView.setBackgroundColor(bgColor);
Accessing color resource in other compiled resources
android:background="@color/background_color"
E.g.,

Project "l04_color"
COMPS413F Supp
#01
15
3. Dimension
Used for specifying the size of a UI component
(e.g., Button, TextView, …) as different kinds
of UI component may have different dimension
values
Should be stored in file
"/res/values/dimens.xml"

Note:
 The file is generated automatically when the project is
created
Tagged by "<dimen>" tag
<?xml version="1.0" encoding="utf-8"?>
E.g.,
COMPS413F Supp
#01
<resources>
<dimen name="thumbDim">100dp</dimen>
</resources>
16
3. Dimension
Note:

Each dimension resource value must end with a unit of measurement

E.g., dp, in, mm, pt, px, sp
Unit of measurement
Density-independent pixels
Inches
Millimeters
Point
Pixels
Scale-independent pixels
Representation
dp
in
mm
pt
px
sp
Description
Pixel relative to a 160 dpi
Physical screen size
Physical screen size
1/72 of an inch based on the physical size of the screen
Corresponds to actual screen pixels
Use for specifying font sizes
Accessing dimension resource programmatically

float textViewDim = getResources().getDimension(R.dimen.textViewDim);
Accessing dimension resource in other compiled resources

E.g.,

android:layout_width="@dimen/textViewDim"
Project "l04_dimension"
COMPS413F Supp
#01
17
b. Drawable Resources
Should be put into the "/res/drawable" folder within the project
directory
Images is the most common drawable resources



E.g., png, jpg, gif, …
They can be used as application icons, button graphics, …
Accessing image resource programmatically
 Images resources are encapsulated in the class "BitmapDrawable"
 Method "getDrawable()" is used to retrieve graphics resource
 E.g., to access image file "res/drawable/animage.png"
BitmapDrawable resultImage = (BitmapDrawable) getResources().getDrawable(R.drawable.animage);

Instead of load the graphic directly, the resource identifier can be used as an
attribute on a UI control component, e.g., "ImageView"
 E.g., load and set the "animage.png" graphics into an "ImageView" component named
"anImageViewer" which defined in layout
ImageView imageView = (ImageView) findViewById(R.id.anImageView);
imageView.setImageResource(R.drawable.animage);

E.g.,
 Project "l04_image"
COMPS413F Supp
#01
18
c. Layouts
UI of an Android application can be defined by layouts, which are specially formatted XML files
Layout resource files are input into the "res/layout" folder within the project directory
E.g., <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/aTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/aString" />
</LinearLayout>

It is the default layout which is created for each Android application

A "LinearLayout" in that acts as a container for all other UI components

Setting the "id" attribute of the "TextView" in "activity_main.xml"


Named "activity_main.xml"

E.g., a TextView

E.g., android:id="@+id/aTextView"

E.g., string resource from "strings.xml"
Referencing other resources in the "main.xml" layout file

@string/aString
Retrieve the TextView object named "aTextView" accordingly
TextView txt = (TextView) findViewById(R.id.aTextView);
COMPS413F Supp
#01
19
d. Files
Files in any format can also be considered as resources in an Android
project

E.g., XML files and raw files
1. XML files

Although Android project can have any kinds of files as the resource, the XML file
resources are the preferred as there are variety of XML utilities available for the
Android platform
 Utility packages
Package
android.sax.*
android.util.Xml.*
org.xml.sax.*
javax.xml.*
org.w3c.dom
org.xmlpull.*


Description
Framework to write standard SAX handlers
XML utilities, including the XMLPullParser
Core SAX functionality (see www.saxproject.org)
SAX and limited DOM
Interfaces for DOM
XmlPullParser and XMLSerializer interfaces (see www.xmlpull.org)
The XML files should be put into the "/res/xml" folder within the project directory
The "getXml()" method can be used to access an XML file programming
 E.g., to access a XML file named "/res/xml/a_xml.xml"
XmlResourceParser defaultDataConfig = getResources().getXml(R.xml.a_xml);
COMPS413F Supp
#01
20
1. XML File – Example – Project
"l04_read_xml"
XmlResourceParser xrp = getResources().getXml(R.xml.a_xml);
xrp.next();
int eventType = xrp.getEventType();
String str = "";
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
str += "--- Start XML ---\n";
}
else if(eventType == XmlPullParser.START_TAG) {
str += "Start tag: "+xrp.getName() + "\n";
}
else if(eventType == XmlPullParser.END_TAG) {
str += "End tag: "+xrp.getName() + "\n";
}
else if(eventType == XmlPullParser.TEXT) {
str += " Text: "+xrp.getText() + "\n";
}
eventType = xrp.next();
}
str += ("--- End XML ---\n");
aTextView.setText(str);
COMPS413F Supp
#01
21
2. Raw Files
Text, audio, and video files can be considered as raw files
These raw files should be put into the "/res/raw" folder
within the project directory
The "openRawResource()" method can be used to access
a raw resource programmatically

E.g., to access a raw file name "/res/raw/text_file.txt"
InputStream iFile = getResources().openRawResource(R.raw.text_file);
Note:

File suffix is excluded for determining if the name of the raw
resource file is unique or not
 E.g., "text_file.txt" and "text_file.dat" would conflict
COMPS413F Supp
#01
22
Default & Alternative Resources
Alternative resources can provide to the application for
supporting specific device configurations, e.g.,


Alternative drawable resources for different screen densities
Alternative string resources for different languages
With alternative resources, device configuration can be detected
and resources can be applied at runtime appropriately
E.g. 1, screen density


Default resources of images for devices of any configuration are
stored in the "res/drawable" directory
For devices with high-density (~240dpi) screens
 Override the default image resources directory with "res/drawable-hdpi"
directory

For devices with low-density (~120dpi) and medium-density
(~160dpi) screens
 Image can be stored in the "res/drawable-ldpi" and "res/drawable-mdpi"
directory respectively
COMPS413F Supp
#01
23
Default & Alternative Resources
E.g. 2, language

Alternative resources can also be specified for languages
and regions
 E.g., -en, -zh, -en-rUS, -zh-rHK, -zh-rTW, -zh-rCN

E.g., you may have default English strings in
"res/values/strings.xml" and strings for devices in Chinese in
"res/values-zh/strings.xml"
A complete list of the alternative resource qualifiers is
available at

http://developer.android.com/guide/topics/resources/providi
ng-resources.html
E.g.,

Project "l04_alternative_resource"
COMPS413F Supp
#01
24