0% found this document useful (0 votes)
6 views11 pages

Unit-5: Application Resources

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

Unit-5: Application Resources

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Unit-5: Application Resources 5

Unit Structure

3.0 Learning Objectives

5.1 Introduction

5.2 What are resources?

5.3 Resource Directory Hierarchy

5.4 Resource Value Types

5.5 Storing Different Resource Value Types

5.6 Accessing Resource Programmatically

5.7 Referencing System Resources

5.8 Let us sum up

5.9 Check your Progress: Possible Answers

5.10 Further Reading

5.11 Assignment

5.12 Activity

136
5.0 Learning Objectives

After studying this unit student should be able to learn:


• Define Application Resources
• List different types of resources
• Understand resource directory hierarchy within android project
• How resources are stored
• Access user resources and system resources programmatically.

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

5.3 Resource Directory Hierarchy

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.

Figure-60: Resource Hierarchy

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.

For example, the /res/drawable-hdpi directory stores graphics for high-density


screens,the /res/drawable-ldpi directory stores graphics for low-density screens, and
the/res/drawable-mdpi directory stores graphics for medium-density screens. If you
had agraphic resource that was shared by all screens, you would simply store that
resource in the/res/drawable directory.

5.4 Resource Value Types

Android applications rely on many different types of resources—such as text strings,


graphics, and color schemes—for user interface design.

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

Resource Type Directory Filename XML Tag


Strings /res/values/ strings.xml <string>
StringPluralization /res/values/ strings.xml <plurals>, <item>
Arrays ofStrings /res/values/ strings.xml <string-array>,<item>
Booleans /res/values/ bools.xml <bool>
Colors /res/values/ Colors.xml <color>
Color StateLists /res/color/ Examples include <selector>, <item>
buttonstates.xml
indicators.xml
Dimensions /res/values/ Dimens.xml <dimen>
Integers /res/values/ integers.xml <integer>

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

Storing Simple Resource Types Such as Strings

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:

<?xml version=”1.0” encoding=”utf-8”?>

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.

Storing Graphics, Animations, Menus, and Files

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.

5.6 Accessing Resource Programmatically

When android application is compiled, a R class gets generated, which contains


resource IDs for all the resources available in your res/ directory. You can use R

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.

Example-1: To access res/drawable/myimage.png and set an ImageView you will


use following code −

ImageViewimageView = (ImageView) findViewById(R.id.myimageview);


imageView.setImageResource(R.drawable.myimage);

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.

Example-2: Consider next example where res/values/strings.xml has following


definition:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello, World!</string>
</resources>

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);

Example-3:Consider a layout res/layout/activity_main.xml with the following


definition

<?xml version="1.0" encoding="utf-8"?>


142
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<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:

public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

Accessing Resources in XML Layout

Consider the following resource XML res/values/strings.xml file that includes a color
resource and a string resource:

<?xml version="1.0" encoding="utf-8"?>


<resources>
<color name="opaque_red">#f00</color>
<string name="hello">Hello!</string>
</resources>
143
Now you can use these resources in the following layout file to set the text color and
text string as follows:

<?xml version="1.0" encoding="utf-8"?>


<EditTextxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/opaque_red"
android:text="@string/hello" />

5.7 Referencing System Resources

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

• Animation sequences for fading in and out


• Arrays of email/phone types (home, work, and such)
• Standard system colors
• Dimensions for application thumbnails and icons
• Many commonly used drawable and layout types
• Error strings and standard button text
• System styles and themes

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) All Android applications is composed of ___________

(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)

5.8 Let us sum up


Android applications rely on various types of resources, including strings, string
arrays,colors, dimensions, drawable objects, graphics, animation sequences,
layouts, styles, andthemes. Resources can also be raw files. Many of these
resources are defined with XMLand organized into specially named project
directories. Both default and alternativeresources can be defined using this resource
hierarchy.

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-a) Both A and B 1-b) True 1-c) True

1-d) False 1-e) False 1-f) True

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

• What are resources?


• Explain Resource Directory Hierarchy
• How can we reference the system resources?
• Explain with example how can we access resource programmatically?

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy