SlideShare a Scribd company logo
Chapter 9
Resources in Android
By
Dr. Ramkumar Lakshminarayanan
Introduction
Resources in Android are files stored under the res directory of your project. Resources
can be physical files (Audio, video, images, text, etc…) or xml files that declare some values to
use in your application. In this chapter we will discuss about the strings and menu resources.
Why use Resources:
1. The resources are bound to the application in a way that you can change them without
needing to change the source code or recompile the application.
2. The Android Generates an ID for each resource file so you can access them directly and
easily. All the resources IDs are added to the R.Java file.
3. When you use XML resource files, Android pareses these files automatically so you can
reference values defined in them directly with no extra effort.
4. Using resources is very useful in Localization and Internationalization of the application
in case you develop multilingual applications. This includes not just the labels but it can
include alignment, directions images or any kind of files.
Types of resources:
 Strings, colors, arrays, dimensions. Defined in res/values/ directory. Using them is very
useful in Localization and Internationalization.
 Images put in res/drawable directory. You can put all the images or icons you need in
your application.
 Animations, defined in res/anime/ directory. You can define animations that for example
correspond to a button click.
 XML, defined in res/xml/ directory for xml files containing your custom data.
 Layout resources, defined in res/layout/ for declaring the views that construct the user
interface of the activities.
String Resources:
Android offers three types of string resources:
1. Plain Strings.
2. String formats.
3. Styled texts.
Plain string resources can be declared in res/values/strings.xml
If you create an Android application [for example call it HelloAndroid] and just before adding
anything, browse to res/values/strings.xml it will be like this:
Hello World, HelloAndroid!
HelloAndroid
This is a pretty basic example of Plain text resources. The resource with name=”app_name” is
the name of the application that appears when you deploy the application to the phone, it’s
referenced in the AndroidManifest.xml file in the application tab. You can change it as you want.
Now let’s add two entries in the file to see how can we use plain text resources within the
application.
This is referenced from the res/layout/main.xml
This is referenced from the code
The first string will be the text of a text view defined in res/layout/main.xml file
See that to reference the first string we use the @string/[Resource Name] convention.
The second resource will be referenced from the code file of the activity like this
TextView txtHeader2=(TextView)findViewById(R.id.txtHeader2);
txtHeader2.setText(getString(R.string.plainResource2));
if you open R.java file of your project you will find that Android has generated a class called
string with members referencing to your string resources:
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
public static final int plainResource1=0x7f040002;
public static final int plainResource2=0x7f040003;
}
Also notice the way you access the string resources in Android, you don’t open the
strings.xml file and parse it to extract the values you want to reference- instead you access them
through the R.string class defined in R.Java and Android does the rest for you.
Also another interesting feature is that you can define your own resources file, go to res/values/
directory and can create it.
Menu Resource
A menu resource defines an application menu (Options Menu, Context Menu, or
submenu) that can be inflated with MenuInflater.
Defining a Menu in XML
For all menu types, Android provides a standard XML format to define menu items.
Instead of building a menu in your activity's code, you should define a menu and all its items in
an XML menu resource. You can then inflate the menu resource (load it as a Menu object) in
your activity or fragment.
Using a menu resource is a good practice for a few reasons:
 It's easier to visualize the menu structure in XML.
 It separates the content for the menu from your application's behavioral code.
 It allows you to create alternative menu configurations for different platform versions,
screen sizes, and other configurations by leveraging the app resources framework.
To define the menu, create an XML file inside your project's res/menu/ directory and
build the menu with the following elements:
<menu>
Defines a Menu, which is a container for menu items. A <menu> element must be the
root node for the file and can hold one or more <item> and <group> elements.
<item>
Creates a MenuItem, which represents a single item in a menu. This element may contain
a nested <menu> element in order to create a submenu.
<group>
An optional, invisible container for <item> elements. It allows you to categorize menu
items so they share properties such as active state and visibility. For more information, see the
section about Creating Menu Groups.
Here's an example menu named game_menu.xml:
The <item> element supports several attributes you can use to define an item's
appearance and behavior. The items in the above menu include the following attributes:
android:id
A resource ID that's unique to the item, which allows the application can recognize the
item when the user selects it.
android:icon
A reference to a drawable to use as the item's icon.
android:title
A reference to a string to use as the item's title.
android:showAsAction
Specifies when and how this item should appear as an action item in the action bar.
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
android:icon="@drawable/ic_new_game"
android:title="@string/new_game"
android:showAsAction="ifRoom"/>
<item android:id="@+id/help"
android:icon="@drawable/ic_help"
android:title="@string/help" />
</menu>
These are the most important attributes you should use, but there are many more
available. You can add a submenu to an item in any menu (except a submenu) by adding a
<menu> element as the child of an <item>. Submenus are useful when your application has a lot
of functions that can be organized into topics, like items in a PC application's menu bar (File,
Edit, View, etc.). For example:
To use the menu in your activity, you need to inflate the menu resource (convert the
XML resource into a programmable object) using MenuInflater.inflate(). In the following
sections, you'll see how to inflate a menu for each menu type.
Summary
In this chapter we discussed about the string and menu resources in Android. In the next
chapter we will discuss about Animation Resource and Color State List Resources.
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item android:id="@+id/file"
android:title="@string/file" >
<!-- "file" submenu -->
<menu>
<item android:id="@+id/create_new"
android:title="@string/create_new" />
<item android:id="@+id/open"
android:title="@string/open" />
</menu>
</item>
</menu>

More Related Content

PPTX
Android Programming.pptx
PPTX
Create an android app for database creation using.pptx
PPTX
03 android application structure
PPTX
Android Layout.pptx
PPTX
Write an application that draws basic graphical primitives.pptx
PPTX
Android development session 3 - layout
PDF
Android ui layout
PPTX
Day 15: Working in Background
Android Programming.pptx
Create an android app for database creation using.pptx
03 android application structure
Android Layout.pptx
Write an application that draws basic graphical primitives.pptx
Android development session 3 - layout
Android ui layout
Day 15: Working in Background

What's hot (20)

PPTX
Android Services
PPTX
Building a simple user interface lesson2
PPTX
Android UI
PPT
android layouts
PPT
View groups containers
PPTX
Client-Server
PPT
Android UI Patterns
PPTX
Android Application Component: BroadcastReceiver Tutorial
PDF
Marakana Android User Interface
PPT
"Android" mobilių programėlių kūrimo įvadas #2
PPT
Day 4: Android: UI Widgets
PDF
01 09 - graphical user interface - basic widgets
PDF
Access Database Manager fro Mac
PPTX
Android Workshop: Day 1 Part 3
PDF
Access Database Manager for iOS (iPhone and iPad)
PDF
Unit 2.5
PDF
Basic Oracle Usage v1
PPT
Londroid Android Home Screen Widgets
PDF
Top 5 User Interface elements for Android App
PPT
Day: 2 Environment Setup for Android Application Development
Android Services
Building a simple user interface lesson2
Android UI
android layouts
View groups containers
Client-Server
Android UI Patterns
Android Application Component: BroadcastReceiver Tutorial
Marakana Android User Interface
"Android" mobilių programėlių kūrimo įvadas #2
Day 4: Android: UI Widgets
01 09 - graphical user interface - basic widgets
Access Database Manager fro Mac
Android Workshop: Day 1 Part 3
Access Database Manager for iOS (iPhone and iPad)
Unit 2.5
Basic Oracle Usage v1
Londroid Android Home Screen Widgets
Top 5 User Interface elements for Android App
Day: 2 Environment Setup for Android Application Development
Ad

Viewers also liked (7)

PPTX
Windows azure session2
PDF
Layout Object in Android App
PDF
Data Mining Techniques using WEKA_Saurabh Singh_10BM60082
PPTX
Amazon web services
DOCX
Avinash Resume
DOCX
Android animation and color state list resources-chapter 10
Windows azure session2
Layout Object in Android App
Data Mining Techniques using WEKA_Saurabh Singh_10BM60082
Amazon web services
Avinash Resume
Android animation and color state list resources-chapter 10
Ad

Similar to Android resources in android-chapter9 (20)

DOCX
Android Resources.docx
PPTX
mobile application development -unit-3-
PPT
Synapseindia android apps introduction hello world
PDF
Basics and different xml files used in android
PPTX
Lesson 10
PPTX
Android Application Fundamentals.
PPT
Android application structure
PPTX
this is PPT for mobail application development
PDF
mad-unit-3.pdfjsjsjhshehdjsjsjjdjdjsjsikwjsjkks
PPT
Unit 2 in environment science and technology
PPTX
Mobile Application Development-Components and Layouts
PDF
Ap quiz app
PPTX
Android ui with xml
DOCX
Android xml-based layouts-chapter5
PPTX
Unit 2 part for information technology1 4.pptx
PPTX
Chapter 2 lesson-1 adding the action bar
DOCX
Android style resource and other resources types-chapter12
PPT
eyeryeryeryeryeyeyeryeryyerye13872085.ppt
PPTX
Learn Xamarin Absolute Beginners - Permissions, Building the App GUI & Menus
PPTX
Lec-3-Mobile Application Development.pptx
Android Resources.docx
mobile application development -unit-3-
Synapseindia android apps introduction hello world
Basics and different xml files used in android
Lesson 10
Android Application Fundamentals.
Android application structure
this is PPT for mobail application development
mad-unit-3.pdfjsjsjhshehdjsjsjjdjdjsjsikwjsjkks
Unit 2 in environment science and technology
Mobile Application Development-Components and Layouts
Ap quiz app
Android ui with xml
Android xml-based layouts-chapter5
Unit 2 part for information technology1 4.pptx
Chapter 2 lesson-1 adding the action bar
Android style resource and other resources types-chapter12
eyeryeryeryeryeyeyeryeryyerye13872085.ppt
Learn Xamarin Absolute Beginners - Permissions, Building the App GUI & Menus
Lec-3-Mobile Application Development.pptx

More from Dr. Ramkumar Lakshminarayanan (20)

PPT
IT security awareness
PPT
Basics of IT security
PDF
IT Security Awareness Posters
PPT
Normalisation revision
PPTX
Windows mobile programming
PPTX
Concurrency control
PPT
Web technology today
PDF
Phonegap for Android
PDF
Create and Sell Android App (in tamil)
PDF
Android app - Creating Live Wallpaper (tamil)
PDF
Android Tips (Tamil)
PDF
Android Animation (in tamil)
PDF
Creating List in Android App (in tamil)
PDF
Single Touch event view in Android (in tamil)
PDF
Android Application using seekbar (in tamil)
PDF
Rating Bar in Android Example
PDF
Creating Image Gallery - Android app (in tamil)
PDF
Create Android App using web view (in tamil)
PDF
Hardware Interface in Android (in tamil)
IT security awareness
Basics of IT security
IT Security Awareness Posters
Normalisation revision
Windows mobile programming
Concurrency control
Web technology today
Phonegap for Android
Create and Sell Android App (in tamil)
Android app - Creating Live Wallpaper (tamil)
Android Tips (Tamil)
Android Animation (in tamil)
Creating List in Android App (in tamil)
Single Touch event view in Android (in tamil)
Android Application using seekbar (in tamil)
Rating Bar in Android Example
Creating Image Gallery - Android app (in tamil)
Create Android App using web view (in tamil)
Hardware Interface in Android (in tamil)

Recently uploaded (6)

DOC
Camb毕业证学历认证,格罗斯泰斯特主教大学毕业证仿冒文凭毕业证
PDF
Lesson 13- HEREDITY _ pedSAWEREGFVCXZDSASEWFigree.pdf
PPTX
ASMS Telecommunication company Profile
PDF
6-UseCfgfhgfhgfhgfhgfhfhhaseActivity.pdf
PPTX
Introduction to Packet Tracer Course Overview - Aug 21 (1).pptx
DOC
证书学历UoA毕业证,澳大利亚中汇学院毕业证国外大学毕业证
Camb毕业证学历认证,格罗斯泰斯特主教大学毕业证仿冒文凭毕业证
Lesson 13- HEREDITY _ pedSAWEREGFVCXZDSASEWFigree.pdf
ASMS Telecommunication company Profile
6-UseCfgfhgfhgfhgfhgfhfhhaseActivity.pdf
Introduction to Packet Tracer Course Overview - Aug 21 (1).pptx
证书学历UoA毕业证,澳大利亚中汇学院毕业证国外大学毕业证

Android resources in android-chapter9

  • 1. Chapter 9 Resources in Android By Dr. Ramkumar Lakshminarayanan Introduction Resources in Android are files stored under the res directory of your project. Resources can be physical files (Audio, video, images, text, etc…) or xml files that declare some values to use in your application. In this chapter we will discuss about the strings and menu resources. Why use Resources: 1. The resources are bound to the application in a way that you can change them without needing to change the source code or recompile the application. 2. The Android Generates an ID for each resource file so you can access them directly and easily. All the resources IDs are added to the R.Java file. 3. When you use XML resource files, Android pareses these files automatically so you can reference values defined in them directly with no extra effort. 4. Using resources is very useful in Localization and Internationalization of the application in case you develop multilingual applications. This includes not just the labels but it can include alignment, directions images or any kind of files. Types of resources:  Strings, colors, arrays, dimensions. Defined in res/values/ directory. Using them is very useful in Localization and Internationalization.  Images put in res/drawable directory. You can put all the images or icons you need in your application.  Animations, defined in res/anime/ directory. You can define animations that for example correspond to a button click.  XML, defined in res/xml/ directory for xml files containing your custom data.  Layout resources, defined in res/layout/ for declaring the views that construct the user interface of the activities. String Resources:
  • 2. Android offers three types of string resources: 1. Plain Strings. 2. String formats. 3. Styled texts. Plain string resources can be declared in res/values/strings.xml If you create an Android application [for example call it HelloAndroid] and just before adding anything, browse to res/values/strings.xml it will be like this: Hello World, HelloAndroid! HelloAndroid This is a pretty basic example of Plain text resources. The resource with name=”app_name” is the name of the application that appears when you deploy the application to the phone, it’s referenced in the AndroidManifest.xml file in the application tab. You can change it as you want. Now let’s add two entries in the file to see how can we use plain text resources within the application. This is referenced from the res/layout/main.xml This is referenced from the code The first string will be the text of a text view defined in res/layout/main.xml file See that to reference the first string we use the @string/[Resource Name] convention. The second resource will be referenced from the code file of the activity like this TextView txtHeader2=(TextView)findViewById(R.id.txtHeader2); txtHeader2.setText(getString(R.string.plainResource2)); if you open R.java file of your project you will find that Android has generated a class called string with members referencing to your string resources: public static final class string { public static final int app_name=0x7f040001; public static final int hello=0x7f040000; public static final int plainResource1=0x7f040002; public static final int plainResource2=0x7f040003;
  • 3. } Also notice the way you access the string resources in Android, you don’t open the strings.xml file and parse it to extract the values you want to reference- instead you access them through the R.string class defined in R.Java and Android does the rest for you. Also another interesting feature is that you can define your own resources file, go to res/values/ directory and can create it. Menu Resource A menu resource defines an application menu (Options Menu, Context Menu, or submenu) that can be inflated with MenuInflater. Defining a Menu in XML For all menu types, Android provides a standard XML format to define menu items. Instead of building a menu in your activity's code, you should define a menu and all its items in an XML menu resource. You can then inflate the menu resource (load it as a Menu object) in your activity or fragment. Using a menu resource is a good practice for a few reasons:  It's easier to visualize the menu structure in XML.  It separates the content for the menu from your application's behavioral code.  It allows you to create alternative menu configurations for different platform versions, screen sizes, and other configurations by leveraging the app resources framework. To define the menu, create an XML file inside your project's res/menu/ directory and build the menu with the following elements: <menu> Defines a Menu, which is a container for menu items. A <menu> element must be the root node for the file and can hold one or more <item> and <group> elements. <item> Creates a MenuItem, which represents a single item in a menu. This element may contain a nested <menu> element in order to create a submenu. <group>
  • 4. An optional, invisible container for <item> elements. It allows you to categorize menu items so they share properties such as active state and visibility. For more information, see the section about Creating Menu Groups. Here's an example menu named game_menu.xml: The <item> element supports several attributes you can use to define an item's appearance and behavior. The items in the above menu include the following attributes: android:id A resource ID that's unique to the item, which allows the application can recognize the item when the user selects it. android:icon A reference to a drawable to use as the item's icon. android:title A reference to a string to use as the item's title. android:showAsAction Specifies when and how this item should appear as an action item in the action bar. <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/new_game" android:icon="@drawable/ic_new_game" android:title="@string/new_game" android:showAsAction="ifRoom"/> <item android:id="@+id/help" android:icon="@drawable/ic_help" android:title="@string/help" /> </menu>
  • 5. These are the most important attributes you should use, but there are many more available. You can add a submenu to an item in any menu (except a submenu) by adding a <menu> element as the child of an <item>. Submenus are useful when your application has a lot of functions that can be organized into topics, like items in a PC application's menu bar (File, Edit, View, etc.). For example: To use the menu in your activity, you need to inflate the menu resource (convert the XML resource into a programmable object) using MenuInflater.inflate(). In the following sections, you'll see how to inflate a menu for each menu type. Summary In this chapter we discussed about the string and menu resources in Android. In the next chapter we will discuss about Animation Resource and Color State List Resources. <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/file" android:title="@string/file" > <!-- "file" submenu --> <menu> <item android:id="@+id/create_new" android:title="@string/create_new" /> <item android:id="@+id/open" android:title="@string/open" /> </menu> </item> </menu>