Unit-5: Application Resources
Unit-5: Application Resources
Unit Structure
5.1 Introduction
5.11 Assignment
5.12 Activity
136
5.0 Learning Objectives
5.1 Introduction
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.
The well-written application accesses its resources programmatically instead of hard
coding them into the source code.This is done for a variety of reasons. Storing
applicationresources in a single place is a more organized approach to development
and makesthe code more readable and maintainable. Externalizing resources such
as strings makes iteasier to localize applications for different languages and
geographic regions.
137
5.2 What are resources?
All Android applications are composed of two things: functionality (code instructions)
anddata (resources).The functionality is the code that determines how your
application behaves.This includes any algorithms that make the application run.
Resources include textstrings, images and icons, audio files, videos, and other data
used by the application.Android resource files are stored separately from the java
class files in the Android project.Most common resource types are stored in
XML.You can also store raw data files
Resources are organized in a strict directory hierarchy within the Android project. All
resourcesmust be stored under the /res project directory in specially named
subdirectoriesthat must be lowercase.Different resource types are stored in different
directories.The resource sub-directoriesgenerated when you create an Android
project are shown in below.
138
Each resource type corresponds to a specific resource subdirectory name. For
example, all graphics are stored under the /res/drawable directory structure.
Resources can be furtherorganized in a variety of ways using even more specially
named directory qualifiers.
These resources are stored in the /res directory of your Android project in a strict
(butreasonably flexible) set of directories and files.All resources filenames must be
lowercaseand simple (letters, numbers, and underscores only).
The resource types supported by the Android SDK and how they are stored within
theproject are shown in table below
139
Resource Type Directory Filename XML Tag
Arrays ofIntegers /res/values/ integers.xml <integer-array>,<item>
Mixed-TypeArrays /res/values/ Arrays.xml <array>, <item>
SimpleDrawables /res/values/ drawables.xml <drawable>
Graphics /res/drawable/ Examples include Supported graphics
icon.png logo.jpg files
or drawable definition
XML files such
asshapes.
TweenedAnimations /res/anim/ Examples include <set>, <alpha>,
fadesequence.xml <scale>, <translate>,
spinsequence.xml <rotate>
Frame-by-Frame /res/drawable/ Examples include <animation-list>,
Animations sequence1.xml <item>
sequence2.xml
Menus /res/menu/ Examples include <menu>
mainmenu.xml
helpmenu.xml
XML Files /res/xml/ Examples include Defined by the
data.xml developer
data2.xml
Raw Files /res/raw/ Examples include Defined by the
jingle.mp3 developer
somevideo.mp4
helptext.txt
Layouts /res/layout/ Examples include Varies. Must be a
main.xml layoutcontrol.
help.xml
Styles andThemes /res/values/ styles.xml <style>
themes.xml
Table-17
140
5.5 Storing Different Resource Value Types
Simple resource value types, such as strings, colors, dimensions, and other
primitives, arestored under the /res/values project directory in XML files. Each
resource file underthe /res/values directory should begin with the following XML
header:
Next comes the root node <resources> followed by the specific resource element
typessuch as <string> or <color>. Each resource is defined using a different element
name.Although the XML file names are arbitrary, the best practice is to store your
resourcesin separate files to reflect their types, such as strings.xml, colors.xml, and
so on. However,there’s nothing stopping the developers from creating multiple
resource files for agiven type, such as two separate xml files called bright_colors.xml
andmuted_colors.xml, if they so choose.
In addition to simple resource types stored in the /res/values directory, you can
alsostore numerous other types of resources, such as animation sequences,
graphics, arbitraryXML files, and raw files.These types of resources are not stored in
the /res/values directory,but instead stored in specially named directories according
to their type. For example,you can include animation sequence definitions in the
/res/anim directory. Makesure you name resource files appropriately because the
resource name is derived from thefilename of the specific resource. For example, a
file called flag.png in the/res/drawable directory is given the name R.drawable.flag.
141
class to access that resource using sub-directory and resource name or directly
resource ID.
During your application development you will need to access defined resources
either in your code, or in your layout XML files. Following section explains how to
access your resources in both the scenarios.
Here first line of the code make use of R.id.myimageview to get ImageView defined
with id myimageview in a Layout file. Second line of code makes use of
R.drawable.myimage to get an image with name myimage available in drawable sub-
directory under /res.
Now you can set the text on a TextView object with ID msg using a resource ID as
follows:
TextViewmsgTextView = (TextView) findViewById(R.id.msg);
msgTextView.setText(R.string.hello);
<TextViewandroid:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
This application code will load this layout for an Activity, in the onCreate() method as
follows:
Consider the following resource XML res/values/strings.xml file that includes a color
resource and a string resource:
You can access system resources in addition to your own resources.The android
packagecontains all kinds of resources, which you can browse by looking in the
android.R subclasses.Here you find system resources for
You can reference system resources the same way you use your own; set the
package nameto android. For example, to set the background to the system color for
darker gray, youset the appropriate background color attribute to
@android:color/darker_gray.
You can access system resources much like you access your application’s
resources. Insteadof using your application resources, use the Android package’s
resources under theandroid.R class.
144
Check your progress-1
(A) Functionality (B) Data (C) Both A and B (D) Neither A nor B
b) You should always externalize app resources from your code, so that you can
maintain them independently (True/False)
c) You can use R class to access that resource using sub-directory and resource
name or directly resource ID. (True/False)
d) You cannot store resources, such as animation sequences, graphics, arbitrary
XML files, and raw files. (True/False)
e) All resources filenames must be in uppercase and simple (True/False)
f) Different resource types are stored in different directories (True/False)
g) You can access system resources much like you access your application’s
resources. (True/False)
Resources are compiled and accessed using the R.java class file, which is
automaticallygenerated when the application resources are compiled. Developers
access applicationand system resources programmatically using this special class.
145
5.9 Check your Progress: Possible Answers
1-g) True
5.10Further Reading
• https://developer.android.com/reference/android/content/res/Resources
• https://developer.android.com/guide/topics/resources/providing-resources
5.11 Assignment
5.12 Activity
• Create String Resource for Title and Welcome message for Main Activity and use
it at design time and programmatically to set at runtime.
146