Lesson 11. The res / values ​​folder. We use program resources.

Lesson 11. The res / values ​​folder. We use program resources.

In this lesson we:

– find out why you need a res / values ​​folder, what you can store and how to use it

The res subfolders are different resource applications. We already know very well layout files in the folder res / layout. I mentioned the folder res / drawable with density suffixes – stored in it pictures. Now let’s look at the folder res / values. It is designed to store resources (constants) of various types. We will look at the types String and Color.

Let’s 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 the file res / values ​​/ strings.xml

We see two elements of type String:

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

app_name – is used by default as the title for the program and Activity. This is indicated in a manifest file that we have not yet parsed.

You can click on these items and see on the right what they are: name (Name) and value (Value)

Name is it ID. It must be unique, and for him in R.java creates a constant so we can access this String element.

If we look at the XML content of the strings.xml file (the tab below is similar to main.xml), we see that everything is transparent and simple. Let’s try and we use the resources.

For starters, let’s create a screen like this main.xml:


The screen is split into two equal halves containing LinearLayout, Button and TextView. for LinearLayout we will specify background colorand for TextView and Button – change text. We implement this through resources. And View-elements upper parts we will manually adjust through properties, and the lower one part will try to configure programmatically.

Let’s create our resource file in the values ​​folder, let’s name myres.

Once created, the file editor opens. To add an element is simple – we press a button Add and choose typeAnd on the right we write name and value. Let’s create 4 String elements and 2 Color elements:

    Верхний текст
    Верхняя кнопка
    Нижний текст
    Нижняя кнопка

For practice, you can create manually, or you can simply paste this text into myres.xml content. Remember to save. Take a look at R.java, make sure that everything appeared here:

Ok, resources are created, we will configure View elements for their use. First top:

llTop – In Properties we find the property Background, Press the select button (three points) in the branch Color highlight llTopColor and click OK

tvTop – for the Text property, open the search box and search for it tvTopText.

btnTop – for the Text property, open the search box and search for it btnTopText.

The top color has changed and the texts have changed to what we indicated in myres.xml.

To change the bottom, we will code. We first find the elements, then assign them values.

public class MainActivity extends Activity {
  /** Called when the activity is first created. */
  public void onCreate(Bundle savedInstanceState) {

    LinearLayout llBottom = (LinearLayout) findViewById(R.id.llBottom);
    TextView tvBottom = (TextView) findViewById(R.id.tvBottom);
    Button btnBottom = (Button) findViewById(R.id.btnBottom);



Note that the method is used to change the text setText. Only this is not the same setText we used when specifying the text directly. this to the entrance accepts ID and we use R.java which stores the ID of all our resources. That is, the methods are called the same, but different parameters are taken at the input. This is normal in Java.

Save, run and check. The texts and colors are now taken from the resource file. You can change the contents of myres.xml (such as the text for the top button), save, launch the application, and see the changes.

Sometimes it is necessary to get the code not the resource ID, but its value. This is done as follows:


The expression will return the text “Bottom Text” corresponding to the String resource with name = tvBottomText.

Finally, let me say a few words about organizing files to store resources. We have now created String and Color resources in one file myres.xml, but we recommend splitting them on different files (Such as strings.xml, colors.xml …), and I will follow this recommendation later. There are reasons for this, we will see later.

Resource names are end-to-end for all files in the res / values ​​folder. That is, you cannot create a resource with the same name and type in different files.

Resource file names can be arbitrary and you can create as many files as you like. R.java will get all the resources from these files.

In the next lesson:

– Consider application logs and pop-up messages

Discuss in the forum [144 replies]

Leave a Comment