Setting Simple Resource Values Using Eclipse


Build your own Android App Dev Empire

Get Instant Access

Developers can define resource types by editing resource XML files manually and using the aapt to compile them and generate the file or by using Eclipse with the Android plug-in, which includes some very handy resource editors.

To illustrate how to set resources using the Eclipse plug-in, let's look at an example. Create a new Android project and navigate to the /res/values/strings.xml file in Eclipse and double-click the file to edit it.Your strings.xml resource file opens in the right pane and should look something like Figure 6.1.

m Android Rcisouicw:; (dotrtull}

l$l <£> <5' $ © <3> O CD A- Atlrfcjl«»«^.«— (Siring ^

tj' tpp.nmrfK lierAgj © Mo [Sbin^ SM 1

ta * qiKt* irt ftrv itrjA vtfm ra. t^c A C4 Ifw wtMfe l&v^f «f mte"! nuîle


Figure 6.1 The string resource file in the Eclipse Resource Editor (Editor view).

There are two tabs at the bottom of this pane.The Resources tab provides a friendly method to easily insert primitive resource types such as strings, colors, and dimension re-sources.The strings.xml tab shows the raw XML resource file you are creating. Sometimes, editing the XML file manually is much faster, especially if you add a number of new resources. Click the strings.xml tab, and your pane should look something like Figure 6.2.

Figure 6.2 The string resource file in the Eclipse Resource Editor (XML view).

Now add some resources using the Add button on the Resources tab. Specifically, create the following resources:

■ A color resource named prettyTextColor with a value of #ff0000

■ A dimension resource named textPointSize with a value of 14pt

■ A drawable resource named redDrawable with a value of #F00

Now you have several resources of various types in your strings.xml resource file. If you switch back to the XML view, you see that the Eclipse resource editor has added the appropriate XML elements to your file, which now should look something like this:

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

<string name="app_name">ResourceRoundup</string> <string name="hello">Hello World, ResourceRoundupActivity</string> <color name="prettyTextColor">#ff0000</color> <dimen name="textPointSize">14pt</dimen> <drawable name="redDrawable">#F00</drawable> </resources>

Save the strings.xml resource file.The Eclipse plug-in automatically generates the file in your project, with the appropriate resource IDs, which enable you to pro-grammatically access your resources after they are compiled into the project. If you navigate to your file, which is located under the /src directory in your package, it looks something like this:

package com.androidbook.resourceroundup; public final class R {

public static final class attr { }

public static final class color {

public static final int prettyTextColor=0x7f050000;

public static final class dimen {

public static final int textPointSize=0x7f060000;

public static final class drawable {

public static final int icon=0x7f020000; public static final int redDrawable=0x7f020001;

public static final class layout {

public static final int main=0x7f030000;

public static final class string {

public static final int app_name=0x7f040000; public static final int hello=0x7f040001;

Now you are free to use these resources in your code. If you navigate to your source file, you can add some lines to retrieve your resources and work with them, like this: import;

String myString = getResources().getString(R.string.hello); int myColor =

getResources().getColor(R.color.prettyTextColor); float myDimen =

getResources().getDimension(R.dimen.textPointSize); ColorDrawable myDraw = (ColorDrawable)getResources(). getDrawable(R.drawable.redDrawable);

Some resource types, such as string arrays, are more easily added to resource files by editing the XML by hand. For example, if we go back to the strings.xml file and choose the strings.xml tab, we can add a string array to our resource listing by adding the following XML element:

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

<string name="app_name">Use Some Resources</string> <string name="hello">Hello World, UseSomeResources</string> <color name="prettyTextColor">#ff0000</color> <dimen name="textPointSize">14pt</dimen> <drawable name="redDrawable">#F00</drawable> <string-array name="flavors"> <item>Vanilla</item> <item>Chocolate</item> <item>Strawberry</item> </string-array> </resources>

Save the strings.xml file, and now this string array named "flavors" is available in your source file, so you can use it programmatically in like this:

String[] aFlavors =


You now have a general idea how to add simple resources using the Eclipse plug-in, but there are quite a few different types of data available to add as resources. It is a common practice to store different types of resources in different files. For example, you might store the strings in /res/values/strings.xml but store the prettyTextColor color resource in /res/values/colors.xml and the textPointSize dimension resource in /res/values/dimens.xml. Reorganizing where you keep your resources in the resource directory hierarchy does not change the names of the resources, nor the code used earlier to access the resources programmatically.

Now let's have a look at how to add different types of resources to your project.

Was this article helpful?

0 0
Easy Software Profits

Easy Software Profits

Here's How Anyone Can Start, Build and Run their Own Successful Software Empire Without Knowing the First Thing About Programming, Scripting Or Any 'Coder' Stuff. There is no doubt about it software sells. Software has a high percieved value and it solves problems for people.  A claim that can't always be made for PDF books, Video products and the like.

Get My Free Ebook

Post a comment