This course will be retired on July 14, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Build an Interactive Story App!
You have completed Build an Interactive Story App!
Preview
String resources are a powerful way to manage string values in Android apps. Using them is a great practice to follow, so we must understand what they are and how to use them.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Now let's address the key name.
0:00
We can avoid typos by
setting the key in one place.
0:02
And then referencing that same definition,
everywhere we want to use the key.
0:04
There are a couple
different ways to do this.
0:08
But I want to show you how to use
a string resource for this scenario.
0:10
Here in our res directory, expand values
and then double click on strings.xml.
0:13
So, what do we have in here?
0:19
Well, like we saw with styles and colors,
we have a resources root element and
0:21
then we have a string element
with a name and a value.
0:25
All right, so what does it mean to
have strings in a file like this?
0:29
How do we use these?
0:32
Well, let's use this
app name as an example.
0:33
Let's say we want to plaster our app name
all over our app, on the app bar label,
0:36
on each activity, in text views and
labels, in messages we write to the log,
0:41
in emails and text messages that
we generate from within the app.
0:45
I could go on and on, but
imagine we had to use a string variable or
0:48
type Signals from Mars every
time we wanted to use it.
0:51
Now, imagine after a while we decided
to change our story to something like,
0:54
I don't know, Puppy Goes to School.
0:58
Now, we need to go and update all those
places we typed Signals from Mars.
1:00
If it's used all over the place,
we might easily miss some.
1:04
Now, let's imagine we decide to
release a Spanish version of our app.
1:07
But we want to call it by its
appropriate Spanish name [FOREIGN].
1:10
What's our plan now?
1:14
Do we release the English version,
make all the changes for Spanish, and
1:16
then release a separate version for that?
1:19
No, this is why string resources are used.
1:21
We leave all of our code alone,
1:24
and let the Android system figure out what
to use for users in different locations.
1:25
String resources give
us two main benefits.
1:29
Number one, strings get defined in
one place where they can be used
1:32
through out the app.
1:35
And number two,
1:36
we can provide language-specific
versions of the strings.xml file
1:37
that can be swapped out automatically
based on the user's region.
1:41
So, before we add anything here,
1:45
where have we seen this
current app_name value before?
1:46
That's right, in our app bar.
1:50
So, this is where we can set it.
1:52
And we should change this to
read Signals from Mars instead.
1:53
So, I'll delete Interactive Story,
type Signals from Mars.
1:56
And if we take a look at our layout file,
activity_main.xml,
2:01
we can see in the app bar that the title
has changed to Signals from Mars.
2:05
Let's go back to strings.xml.
2:10
And we can get back to that key that
we are using to pass data from one
2:12
activity to another.
2:15
Because our key is a string and
it can be used in multiple places,
2:17
this is a perfect place to define it.
2:20
So, let's add a new item.
2:22
I'll create a new line,
add a string element,
2:24
give it the name of
key_name in all lowercase.
2:27
Then we can type the value.
2:32
We just used name, all lowercase.
2:33
And now,
we have a new string resource to use.
2:35
And coming up next, we'll see how to
access this inside of our activities.
2:38
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up