A string resource is a text-based value that's defined in a values resource XML file. Learn about the two major advantages of string resources: you can define a string once and use it multiple times throughout an application, and you can create translated string resource definitions for different languages.
- [Instructor] I've previously described the use of string resources, to define strings that can be reused throughout the application. In this xml layout file, in the project string resources, my last text view component at the bottom of the screen, is using a string resource. The actual value is showing up in text view, but if I move the cursor over it, I see a string resource ID. The ID starts with at string slash, and then there's a key that references the actual resource. You can click into that ID and you'll see the underlying resource ID, and then hold down control on Windows, or command on Mac, and click to jump to the resource definition.
You can change the value here and it will then change wherever it's referenced throughout the application. The strings.xml file has root element of resources, each resource has an element named string, with an attribute named name. The value goes between the tags. You can edit the xml file directly here, or you can click on the open editor link and you can then add and remove resources visually. So for example, I'll type in a key of apple underscore pie, and give it a value of apple pie.
Now click okay, then I'll go back to activity main dot xml. I can fill in the resource in a couple of different ways. If I want to do it in code, I can delete a literal string, and then start typing at string slash, and then choose the resource I want to use. Alternatively, I'll undo that change, and then go back to design view, I'll click on the text view component, and then in the properties window, under text, I'll click on the browser icon.
After a few moments, that will open this dialogue showing me my resources. I can choose the one I want, and click okay, and it'll be filled in, in exactly the same way. If you want to rename a resource, you can refactor it. Click into the ID and press shift F6, and then change it here. I'll call this, delicious apple pie. As with all resource ID's, the ID has to be lowercase, with no spaces or special characters other than underscores.
I'll change I there, and because I'm refactoring and not just retyping, it'll be changed in the reference as well, and anywhere else it occurs throughout the application. Here's another way of creating a string resource. In the xml layout file, I already have a string, I'll place the cursor inside the string and then press alt enter, or option and return, for an intention action. I'll choose extract string resource. The resource name was set automatically for me in this case, but I'll change the resource value to dollar nine dot zero zero.
Now I'll look at this in design view, and I'll see the text view displays that value. You can also work with string resources in Java code. For example, I'll go to strings dot xml, and first I'm going to refactor this resource ID again. I'll press shift F6, and just change it to delicious pie. Then, I'll change the value, so that instead of apple, I'll put in a place holder of percent S. This is a place holder that's compatible with Java's string formatting capabilities.
Now I'll go back to main activity, and in the on create method, as the activity starts up, I'll get a reference to that text view component. I'll go back to my xml layout file, and I'll look to make sure I know what the ID of the text view is. Here's the text view that's displaying the delicious pie reference. I'm going to refactor this text view component, to product name, so it's easier to work with in my Java code, now I'll come back to my main activity class, the on create method and I'll create a reference to a text view component.
The type of the variable I'm creating is text view, and it's a member of the package android dot widget. I'll name this product text, and I'll get it's reference by calling the method find view by ID. Find view by ID takes a resource ID that starts with uppercase R, then dot ID, and then the ID of the text view component you're looking for. Any time you call find view by ID, it'll return a view by default, that's the super class of text view and button and a few others, and you need to cast the result as the appropriate type.
Move the cursor back a few characters, and use an intention action, and choose cast to, and then the appropriate type, and the Java code will be added for you. Now I'll create a string that I'll call product name, and I'll use the Java method, string dot format. I need to get that string value from the resources. I can't just use an ID, I have to get an actual string, and there's a method for that, called get string. That takes the resource ID, and I'll pass in R dot string, dot delicious pie.
That string, remember has a place holder in it, so I'll pass in a second argument of, apple. Finally I'll apply that string to the text view component, with product text dot set text, and I'll pass in product name, and now I'm ready to run the code. I'm now running an emulator that I can use on screen, it's a Nexus 6P running Android 7.1. And so now, I'm showing apple pie. I'll go back to my code, and I'll change apple to cherry, and I'll run the code again.
And there's the result, it now says cherry pie. So string resources are a great way to create reusable values that you can use throughout the application, but they're also critical when you decide you want to localize your applications. That is, have it look a little bit different in different languages. In order to do that, you would create multiple versions of each strings dot xml file, one for each language you want to support. You'd have one for English, one for French, on for German, and so on. And then you would provide the translated values in each copy of the file.
Actually implementing those translations is beyond the scope of this course, but it's an important factor if you decide you want to deliver your application in different human languages. Regardless, string resources are a critical part of the Android framework, and they let you easily define all of your strings in one place, and then use them throughout the application.
- Using View components
- Exploring the design repository
- Using the new ConstraintLayout component
- Adding views to a ViewGroup with Java
- Manage string values as resources
- Working with TextView, EditText, and TextInputLayout
- Displaying toast and Snackbar messages
- Defining and applying styles
- Using material design components
- Managing image display with Picasso