14.0 Application Resources-2
14.0 Application Resources-2
14.0 Application Resources-2
Learning Objectives
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
application resources in a single place is a more organized approach to development
and makes the code more readable and maintainable. Externalizing resources such
1
as strings makes it easier to localize applications for different languages and
geographic regions.
All Android applications are composed of two things: functionality (code instructions)
and data (resources).The functionality is the code that determines how your
application behaves. This includes any algorithms that make the application run.
Resources include text strings, 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
resources must be stored under the /res project directory in specially named
subdirectories that must be lowercase. Different resource types are stored in
different directories. The resource sub-directories generated when you create an
Android project are shown in below.
2
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 further organized 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
(but reasonably flexible) set of directories and files. All resources filenames must be
lowercase and simple (letters, numbers, and underscores only).
The resource types supported by the Android SDK and how they are stored within
the project are shown in table below
3
Resource Type Directory Filename XML Tag
Integers /res/values/ integers.xml <integer>
Arrays of Integers /res/values/ integers.xml <integer-array>,<item>
Mixed-Type Arrays /res/values/ Arrays.xml <array>, <item>
Simple Drawables /res/values/ drawables.xml <drawable>
Graphics /res/drawable/ Examples include Supported graphics files
icon.png logo.jpg or drawable definition
XML files such as
shapes.
Tweened /res/anim/ Examples include <set>, <alpha>,
Animations 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 layout
main.xml control.
help.xml
Styles and Themes /res/values/ styles.xml <style>
themes.xml
Table-1
4
Storing Different Resource Value Types
Simple resource value types, such as strings, colors, dimensions, and other
primitives, are stored under the /res/values project directory in XML files. Each
resource file under the /res/values directory should begin with the following XML
header:
Next comes the root node <resources> followed by the specific resource element
types such 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
resources in 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 a given type, such as two separate xml files called
bright_colors.xml and muted_colors.xml, if they so choose.
In addition to simple resource types stored in the /res/values directory, you can also
store numerous other types of resources, such as animation sequences, graphics,
arbitrary XML 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. Make sure you name resource files appropriately because the
resource name is derived from the filename of the specific resource. For example, a
file called flag.png in the /res/drawable directory is given the name R.drawable.flag.
5
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:
TextView msgTextView = (TextView) findViewById(R.id.msg);
msgTextView.setText(R.string.hello);
<TextView android: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
package contains 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 name to android. For example, to set the background to the system color
for darker gray, you set the appropriate background color attribute to
@android:color/darker_gray.
You can access system resources much like you access your application’s
resources. Instead of using your application resources, use the Android package’s
resources under the android.R class.
8
Let us sum up
Resources are compiled and accessed using the R.java class file, which is
automatically generated when the application resources are compiled. Developers
access application and system resources programmatically using this special class.
Further Reading
https://developer.android.com/reference/android/content/res/Resources
https://developer.android.com/guide/topics/resources/providing-resources
Activity
Create String Resource for Title and Welcome message for Main Activity and use
it at design time and programmatically to set at runtime.