android resources in android-chapter9

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

Upload: ramkumar-lakshminarayanan

Post on 27-Nov-2014

92 views

Category:

Mobile


1 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: Android resources in android-chapter9

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:

Page 2: Android resources in android-chapter9

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;

Page 3: Android resources in android-chapter9

}

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>

Page 4: Android resources in android-chapter9

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>

Page 5: Android resources in android-chapter9

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>