Google+

Android4Beginners.com

Apps development for Android step by step

Lesson 1.2: Keeping text in a dedicated file (why and how to use strings.xml file in Android)

Our first app displays “I’m the best” on the screen. To achieve that goal we’ve used TextView tag and modified its android:text attribute. This attribute could keep a specific text (that we did in previous lesson) or reference to one of strings in a file dedicated for keeping all text.

The file named strings.xml is in values folder (a subfolder of resources). Open it. Mind that there are now two file name tabs (string.xml and main_activity.xml) so you could easily switch between them. Inside there are already some  <string> tags with attribute called “name”.

All text information or instructions used in your Android app should be stored in strings.xml file.

All text information or instructions used in your Android app should be stored in strings.xml file.

To use this file to define strings for your app you have to do two things.

Step 1: Add your own <string> tag to strings.xml file. Remember that it has to be opened and closed. And there is one thing more to remeber: you could not directly use apostrophe in a string (if you try to run such an app you will see an error – Gradle: Apostrophe not preceded by \). There are two options: just put \ character before every ‘ or take whole string into quotes (“”). And if you want tu use quotes inside string, just use \ before them.

It could look like this:

The attribute called name is used to identify this specific text in all parts of your app. Note that name can’t have spaces inside nor characters like ?, “, /, \.

This is a full new code :

Positions of string tags don’t matter, so you could paste it for instance on third position before the line with Hellow world! By the way, if we don’t need this Hello world! text anymore, we could delete the whole string tag containing it.

Step 2: Go back to main_activity.xml file (it should be still open – just click a tab with its name). Now instead of the specific text as value of android:text attribute write: @string/my_best_text (or the name you have used for the string).

Sign @ refers to resources. Then you specify resource type (string in this example) and after / you have to give resource name.

Now you could again look at preview or run the app. The effect should be the same (of course if you have written the same text in both cases).

We are going to use mainly this approach, but in very simple applications there is nothing wrong to write a real text directly into android:text attribute.

Helpful tip: Android Studio suggests most suitable tags, attributes, values etc. during writing a code.  If suggestion is right, just press Tab or Enter (if there are more suggestions, first mark the proper one).

Android Studio often knows what you need: it’s suggestion system makes developing much faster.

Android Studio often knows what you need: it’s suggestion system makes developing much faster.

Previous:
Next: