Getting Resources from the Context4:20 with Ben Jakuben
Once we create some string resources in strings.xml, we need to then access them in our app using the Context.
Let's go back to mainactivity.java, and now to get the string value 0:00 from the strings that XML file, we need to reference it through its name. 0:04 Just like when we reference elements in the layout through their IDs. 0:07 Let's take a brief look at the Android documentation. 0:11 This section talks about how to access string and other types of resources. 0:14 If we scroll down a little bit we can see an example of how to 0:18 access a string in code with the ID r.string.hello, or 0:21 in XML with atstring/ and then the string name that we provided. 0:26 This next section is called accessing resources in code. 0:30 This has an example, with a nice little shortcut, but 0:33 I wanna highlight this line here. 0:35 You can also retrieve individual resources using methods in Resources, 0:37 which you can get an instance of with getResources. 0:42 So there is a special Resources object we can use 0:45 to access all the resources we create and wrap up interact, very cool. 0:48 Let's use this resources object. 0:52 And also use the shortcut shown in this example. 0:53 The resource's object is available to us anywhere in our app, 0:56 if we have access to the current context. 0:59 Do you remember what the context is inside of an activity? 1:01 How do we access the current context here? 1:04 That's right, all activities are a subclass of the context class. 1:07 So we can always access the context directly in an activity. 1:11 Let's spell this out with the variables, 1:15 although we could put this all on one line if we'd prefer. 1:16 You can clean your code up after this explanation if you'd like. 1:18 So here in our startStory method, before we try and use the key name, 1:21 let's add a new Resources variable named resources, and 1:26 we can set it with a method from the context class. 1:31 Thanks to inheritance that means we can just type it here getResources and 1:34 sure enough it returns a resources variable. 1:38 Next let's add a string variable to hold our key name String key = and 1:41 now we can set this from the resources instance resources.. 1:45 And then an auto-complete, let's see there's a getString method and 1:49 it takes an id as a parameter. 1:53 So here we can type R., and 1:55 then notice these are all the different types of resources we can provide. 1:57 We want a string, 2:01 and then from there we can select the new name that we enter key_name. 2:02 Lastly, before we forget, 2:07 we want to change the hard coded key here to use our new key variable. 2:08 Now let's do the same thing in StoryActivity.java. 2:12 Only this time we'll use a shortcut available from the activity class. 2:16 So once again we can delete the hard coded key name here, 2:20 and we can call directly getString() and pass in an ID R.string.key_name. 2:25 So this method is an additional activity method that goes through the given 2:32 resources object and 2:36 let's us access resources using the ID just like we did before. 2:38 All right, let's run this and make sure that it works. 2:41 Okay so if we add our name, start our adventure, we should see it in the log. 2:45 I'll bring it up here. 2:50 And here we go, here's the name that we just entered. 2:52 Okay, there's one more thing I wanna do before we wrap up this video. 2:54 So remember in our layouts before, we had a couple of warnings that we were 2:58 ignoring, that's because I wanted to use string resources to fix them. 3:01 So here, if we go back to the main layout, and 3:05 it's easier to see here in the XML view. 3:07 So first one is the content description. 3:09 And we can now use a quick fix to pull this hard coded string directly into 3:11 a string resource. 3:16 If we hit alt + Enter to do a quick fix anywhere in the hard coded string 3:18 we can select Extract string resource. 3:22 It does everything for us pretty much automatically. 3:25 It gives us a default resource name, 3:27 it gives us the value directly from the hard coded string and 3:29 we're gonna leave the source set alone as main and the file name as strings.xml. 3:32 And you can see it's going to create it in the values directory, 3:37 just like we already have it, so click OK. 3:40 It replaces it here. 3:42 This is how we access it in XML, @string/ and then the string name. 3:43 And then if we go to strings.xml, 3:48 we can see we have a new extracted string resource, cool. 3:49 So as a challenge for you, go through the rest of your layout files, and 3:53 extract all the other warnings into new string resources. 3:56 Okay so we're off to a great start with this app. 4:00 We've introduced multiple activities, and 4:02 now we know how to pass data from one to another using an intent. 4:04 Next step we will build the story part of our app following a really common software 4:07 design pattern, known as Model View Presenter or MVP for short. 4:11 It's pretty awesome, so make sure you're ready for 4:15 the fun before going on the next section. 4:18
You need to sign up for Treehouse in order to download course files.Sign up