[Android] Beginner Tutorials – Lesson 11. res/values folder. Using application resources

In this lesson we will:

– find out what is res/values folder needed for, what can you store in it and how to use it.

Translated by Taras Leskiv (http://android-by-example.blogspot.com/)

In the res folder, subfolders of different application resources are stored. We already have a good knowledge about layout-files in res/layout folder. I also mentioned about res/drawable folder with density suffixes – images are stored in it. Now pay attention to res/values folder. It is intended for resources (constants) storage of different types. We will look through String and Color types.

Create a project:

Project name: P0111_ResValues
Build Target: Android 2.3.3
Application name: ResValues
Package name: ru.startandroid.develop.resvalues
Create Activity: MainActivity

Open res/values/strings.xml file:

20110822_L0011_L_strings

We can see two elements of the String type:

hello – is used by default in the Text property of the TextView in main.xml. And correspondingly, TextView displays the value of this element.

20110822_L0011_L_hello

app_name – is used by default as a title for your application and Activity. This constant is used in the manifest-file, but we are not familiar with it yet.

You can click on these elements and see what do they represent on the right: Name and Value

20110822_L0011_L_helloNameValue

Name – it is an ID. It must be unique and a constant is generated for it in R.java, so we could access this String-element from code.

20110822_L0011_L_R

If we have a look at XML inside strings.xml file (tab at the bottom – the same as for main.xml), we can see that everything is clear and simple there. Let’s try using resources.

To begin with, create this layout in main.xml:

The screen is divided into two equal parts, which contain LinearLayout, Button and TextView. We will specify a background color for LinearLayout and change text for TextView and Button. Let’s implement this using resources. And we will configure View-elements in the top part manually using properties, the bottom part – programmatically.

Let’s create our resource file in the values folder and name it myres.

20110822_L0011_L_createValues

20110822_L0011_L_myresCreate

After creating a file, the editor opens. It is simple to add an element – click Add button, choose the type and write name and value on the right. We will create 4 String-elements and 2 Color-elements:

You can input this text manually for practice, or you can just copy this text into myres.xml. Don’t forget to save the file. Have a look into R.java and make sure that everything appeared there:

20110822_L0011_L_R1

Ok, now when all the resources are created, let’s configure View-elements to use them. Elements on the top for beginning:

llTop – find Background property in Properties and click the selection button (the ellipsis), in the Color branch highlight llTopColor and click OK

20110822_L0011_L_lltop

tvTop – for Text property open the selection window and find tvTopText there.

btnTop – for Text property open the selection window and find btnTopText there.

The color of the top part of the screen has changed and the inscriptions have changed to those we specified in myres.xml.

To change the bottom part of the screen, we will write some code. First, find the elements, then set their values.

Note that for changing text setText method is used. But it is not the same setText that we used when setting text directly. This method takes an ID as a parameter and we use R.java, which stores all our resource IDs. As you see methods have the same name but have different parameters. This is common in Java.

Save, run and check. Now texts and colors are taken from the resource file. You can change the contents of myres.xml (text of the upper button, for instance), save, run the application and you will see your changes.

Sometimes it is required to get the value of the resource in code, not its ID. It is accomplished this way:

This expression will return text “Bottom text”, which corresponds to the String resource with name = tvBottomText.

In conclusion, I will tell a few words about files organization for storing resources. We have just created String and Color resources in a single file myres.xml, but it is recommended to split them into different files (e.g. strings.xml, colors.xml …) and I will follow this recommendation further. There are reasons for that and we will have evidence for this in future.

Resource names are prevailing for all the files in res/values folder. So you cannot create a resource with the same name and type in different files.

You can name resource files as you wish and you can create as many files as you need. All the constants for resources from these files will be generated in R.java.

Comments

comments

Share this Post :

No comments yet.

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *