Alright so there's something else we can do with string resources, and we can have a look at that by making
a couple of changes in PhotoDetailsActivity.
dot Kotlin, so let's actually have a look at that.
So I'm going to open our file, PhotoDetailsActivity. Now something that you'll often want to do, is display
values with a heading to show what the value is.
Now I'm going to demonstrate that by showing the obvious way you might do it, and then we'll put things
right.
Then we come down here to photo
underscore title dot text,
and before the photo dot title I'm going to add double quotes title colon space
plus, and obviously I've got a closing double quote there.
And on the next line for tags, we're going to do the same thing, or similar thing,
double quotes and tags colon in the middle of that, and a space, then plus photo dot tags.
Now that was obviously a very simple change, but Android Studio is highlighting those two lines and giving
a warning. If we hover over that, we can see that it says "Do not concatenate text displayed with setText.
Use resource string with placeholders", and then moving on, "String literal in setText cannot be translated.
Use Android resources instead".
And we get the same warning for both the title and the tags, similar warning, or same warning, and the suggestion
again is to use a string resource instead of hardcoding the text.
Now that's fine. We know how to do that now, but the warning's also talking about a resource string
with placeholders.
So let's actually see how they work, and this'll also let us see how to access string resources in code,
rather than just in the layouts.
So we're going to open up our strings.xml file again. We're going to create a new string resource called photo
underscore title underscore text, and I'll add that just before the closing resources tag.
So less than sign to start off, string space name equals double quotes, and this one's going to be called photo
underscore title underscore text, and we've got a greater than sign to close off the tag, and in the middle there we're going
to type in title colon space percent sign lower case s.
So here the percent
s is a place holder that can be replaced with the required value when the app's running.
The s in that specifies that it can be replaced with a string.
If we'd used a present d instead, then it could be replaced with a decimal number.
So let's add another one for the tags attribute as well.
I'm going to copy that and paste that, and change the text to photo tags text for the name, and the value's going to be tags
colon
space percent s. Alright,
so that's strings.xml updated.
Let's see how to use those resources in code. So back to PhotoDetailsActivity.
I'm going to comment that these two lines. We're going to replace those instead with, for the first one, for the title, it's going
to be photo
underscore title dot text is equal to resources dot getString, parentheses as you can see there, R.string dot
photo underscore title underscore text comma
photo dot title.
That's the first one, and for tags we'll do a similar thing, photo underscore tags dot text is equal to
resources
dot getString parentheses R.string dot,
this times it's going to be photo underscore tags dot text, tags underscore text I should say,
comma photo dot tags.
So we start off by getting the resources for the app, that's this resources part here.
Now resources is a property of activities, and it lets us access the app's resources. So next we're using the get
String function to get the string with the ID photo underscore title underscore text.
Now that's the string we just created in the strings.xml resource file.
Now the getString function will replace any placeholders with the appropriate value, and we get that
by using the title field
of our photo object, over here to the right. And the tags heading works exactly the same,
using getString to get the string resource, this time called photo underscore tags underscore text,
and it replaces the placeholder with the photo's tags field.
So the getString function takes care of replacing the place
holders with the values that we specify, and in this particular case there's only one placeholder to replace
here.
But you can replace several values at once, with a single call to the getString function
if you need to, and it's the same mechanism as the string.format function that we used in the top
10 downloader app, to display whether we were showing the top 10 or top 25 results.
Now that's slightly harder than how we did it in the layout, because Android Studio takes care of
retrieving the string resources for us when it inflates the xml layout. It does give more flexibility
here though, because it allows the use of placeholders to include different text while the app's running.
Of course you can't do that with a layout.
Now there is one other thing you should consider, and that's the fact that words can appear in different
orders in different languages.
Now as an example, my red car would translate to ma voiture rouge in French, and apologies to anyone
speaking French out there.
That was my interpretation anyway. In the English version, the adjective red comes before the noun car,
but in French the word for car comes first.
So a literal translation back to English would be my car red.
Now that means that you may need to apply the values in different orders, in different languages.
Now to cater for that, it's a good idea to actually number the placeholders.
Now to see what I mean, we're going to change the strings.xml file to include placeholder numbers.
So I'll go back to strings.xml, and let's create another entry for the resource.
So less than sign then string to get started, name equals two double quotes, and it's going to be photo underscore
author underscore text, then close that tag.
And this time the new resource is going to contain three placeholders,
but these are going to be numbered, and to number them, percent 1 dollar s space,
percent 2 dollar s space, and percent
3 dollar s,
as you can see.
So we've added three place holders there that are numbered, and we'll just go back to PhotoDetailsActivity, and
what we'll do is we'll replace the text for the photo author in PhotoDetailsActivity. So I'll comment that line out,
and it'll be photo
underscore author dot text is equal to resources.getString parentheses. It's going to be R.string dot
photo underscore author underscore text comma
and let's just try in double quotes, my. Now
one nice touch
here is that Android Studio's picked up on the fact that the resource string has got three placeholders, but we're
only supplying one, so we're getting a value,
we're getting an error.
And if I hover over that we can see that, "requires 3 but format call supplies 1.
So what this does is it prevents us from accidentally providing the wrong number of values when calling
the getString function.
So I'd better fix that.
We'll go back and add the other two.
So I'm going to add a comma space, in double quotes red, double quote, closing the double quote that is, comma and the last
one, double quotes
car, and you can see when I do that the error then disappears.
Now I'm supplying the three strings, my, red and car here,
but obviously in a real app, these would be values that change when the app runs.
This is just to demonstrate how to use placeholder numbering, to change the order of the words for different
languages
if you need to.
So when I run this app, let's do that now.
OK we'll just go into a photo, and
you can see at the top here, my red car.
So basically getting that text for the author.
Now if we needed the words in a different order for another language, we can actually position the numbered
placeholders in any order we want, in that language's strings.xml file.
So as an example, if I go into this one,
and change this and we'll move the third place holder, paste it in and change the order like so. So that's now placeholder one,
three then two.
And now when I run the app again, and load it up,
and you can see over here now we've got the text my car red, and that would then work fine in French.
And we haven't changed the code to do that.
Android would automatically use the French resource file if we'd created one on a French device.
Now the French resource would have the placeholders listed in a different order, like I did when I swapped
the second placeholder, the percent 2 dollar s
with the percent 3 dollar s. And if you're using more than one placeholder,
it's a good idea to always number them, so that your string resources are as flexible as possible.
Alright so I'm going to put the PhotoDetailsActivity back to how it should be.
And basically what I'm going to do is leave the photo author text tag in a string resources,
so it's available if you want to download my source code. So I'm just going to undo that basically, comment this one out, and we'll go back
to the original author name.
And again as I mentioned I've going to leave this in the strings xml file, but as we're not going
to use it
I should really delete it.
And it is a good idea to be tidy and not normally leave unused resources in the xml files.
But as I said I'll leave it in there,
so that's available if you want to download my source code, but it's a good habit to get into, to delete
any unused resources.
Alright.
So at this point we've got a pretty cool looking app for displaying flickr photos, but it does have one
serious shortcoming, and that is that it can only display photos that are tagged with Android and Oreo.
That's pretty boring,
so the last bit of functionality we need to provide, is a search feature to allow users to search on
different tags.
So we'll make a start on that in the next video.