Setting Simple Resource Values Using Eclipse Android

Developers can define resource types by editing resource XML files manually and using the aapt to compile them and generate the R.java 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.

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 resources. 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.

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:

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

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

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:

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

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 resourcedirectory 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.