1 00:00:00,370 --> 00:00:04,370 Let's go back to MainActivity.java, and now to get the string value 2 00:00:04,370 --> 00:00:07,870 from the strings.xml file, we need to reference it through its name. 3 00:00:07,870 --> 00:00:11,620 Just like when we reference elements in the layout through their IDs. 4 00:00:11,620 --> 00:00:14,050 Let's take a brief look at the Android documentation. 5 00:00:14,050 --> 00:00:18,000 This section talks about how to access string and other types of resources. 6 00:00:18,000 --> 00:00:21,610 If we scroll down a little bit we can see an example of how to 7 00:00:21,610 --> 00:00:26,230 access a string in code with the ID r.string.hello, or 8 00:00:26,230 --> 00:00:30,480 in XML with @string/ and then the string name that we provided. 9 00:00:30,480 --> 00:00:33,700 This next section is called accessing resources in code. 10 00:00:33,700 --> 00:00:35,690 This has an example, with a nice little shortcut, but 11 00:00:35,690 --> 00:00:37,790 I wanna highlight this line here. 12 00:00:37,790 --> 00:00:42,160 You can also retrieve individual resources using methods in Resources, 13 00:00:42,160 --> 00:00:45,390 which you can get an instance of with getResources. 14 00:00:45,390 --> 00:00:48,370 So there is a special Resources object we can use 15 00:00:48,370 --> 00:00:52,265 to access all the resources we create and wrap up in our app, very cool. 16 00:00:52,265 --> 00:00:53,780 Let's use this resources object. 17 00:00:53,780 --> 00:00:56,400 And also use the shortcut shown in this example. 18 00:00:56,400 --> 00:00:59,587 The resource's object is available to us anywhere in our app, 19 00:00:59,587 --> 00:01:01,702 if we have access to the current context. 20 00:01:01,702 --> 00:01:04,550 Do you remember what the context is inside of an activity? 21 00:01:04,550 --> 00:01:07,070 How do we access the current context here? 22 00:01:07,070 --> 00:01:11,360 That's right, all activities are a subclass of the context class. 23 00:01:11,360 --> 00:01:15,130 So we can always access the context directly in an activity. 24 00:01:15,130 --> 00:01:16,210 Let's spell this out with the variables, 25 00:01:16,210 --> 00:01:18,920 although we could put this all on one line if we'd prefer. 26 00:01:18,920 --> 00:01:21,980 You can clean your code up after this explanation if you'd like. 27 00:01:21,980 --> 00:01:26,030 So here in our startStory method, before we try and use the key name, 28 00:01:26,030 --> 00:01:31,500 let's add a new Resources variable named resources, and 29 00:01:31,500 --> 00:01:34,590 we can set it with a method from the context class. 30 00:01:34,590 --> 00:01:38,550 Thanks to inheritance that means we can just type it here getResources and 31 00:01:38,550 --> 00:01:41,240 sure enough it returns a resources variable. 32 00:01:41,240 --> 00:01:45,598 Next let's add a string variable to hold our key name String key = and 33 00:01:45,598 --> 00:01:49,520 now we can set this from the Resources instance resources.. 34 00:01:49,520 --> 00:01:53,523 And then an auto-complete, let's see there's a getString method and 35 00:01:53,523 --> 00:01:55,680 it takes an id as a parameter. 36 00:01:55,680 --> 00:01:57,510 So here we can type R., and 37 00:01:57,510 --> 00:02:01,140 then notice these are all the different types of resources we can provide. 38 00:02:01,140 --> 00:02:02,030 We want a string, 39 00:02:02,030 --> 00:02:07,170 and then from there we can select the new name that we enter key_name. 40 00:02:07,170 --> 00:02:08,460 Lastly, before we forget, 41 00:02:08,460 --> 00:02:12,970 we want to change the hard coded key here to use our new key variable. 42 00:02:12,970 --> 00:02:16,050 Now let's do the same thing in StoryActivity.java. 43 00:02:16,050 --> 00:02:19,060 Only this time we'll use a shortcut available from the activity class. 44 00:02:20,180 --> 00:02:25,242 So once again we can delete the hard coded key name here, 45 00:02:25,242 --> 00:02:32,579 and we can call directly getString() and pass in an ID R.string.key_name. 46 00:02:32,579 --> 00:02:36,849 So this method is an additional activity method that goes through the given 47 00:02:36,849 --> 00:02:38,070 resources object and 48 00:02:38,070 --> 00:02:41,744 lets us access resources using the ID just like we did before. 49 00:02:41,744 --> 00:02:43,510 All right, let's run this and make sure that it works. 50 00:02:45,220 --> 00:02:50,440 Okay so if we add our name, start our adventure, we should see it in the log. 51 00:02:50,440 --> 00:02:51,070 I'll bring it up here. 52 00:02:52,410 --> 00:02:54,960 And here we go, here's the name that we just entered. 53 00:02:54,960 --> 00:02:58,060 Okay, there's one more thing I wanna do before we wrap up this video. 54 00:02:58,060 --> 00:03:01,250 So remember in our layouts before, we had a couple of warnings that we were 55 00:03:01,250 --> 00:03:05,200 ignoring, that's because I wanted to use string resources to fix them. 56 00:03:05,200 --> 00:03:07,790 So here, if we go back to the main layout, and 57 00:03:07,790 --> 00:03:09,830 it's easier to see here in the XML view. 58 00:03:09,830 --> 00:03:11,669 So first one is the content description. 59 00:03:11,669 --> 00:03:16,427 And we can now use a quick fix to pull this hard coded string directly into 60 00:03:16,427 --> 00:03:18,350 a string resource. 61 00:03:18,350 --> 00:03:22,430 If we hit alt + Enter to do a quick fix anywhere in the hard coded string 62 00:03:22,430 --> 00:03:25,310 we can select Extract string resource. 63 00:03:25,310 --> 00:03:27,170 It does everything for us pretty much automatically. 64 00:03:27,170 --> 00:03:29,389 It gives us a default resource name, 65 00:03:29,389 --> 00:03:32,969 it gives us the value directly from the hard coded string and 66 00:03:32,969 --> 00:03:37,929 we're gonna leave the source set alone as main and the file name as strings.xml. 67 00:03:37,929 --> 00:03:40,689 And you can see it's going to create it in the values directory, 68 00:03:40,689 --> 00:03:42,541 just like we already have it, so click OK. 69 00:03:42,541 --> 00:03:43,650 It replaces it here. 70 00:03:43,650 --> 00:03:48,110 This is how we access it in XML, @string/ and then the string name. 71 00:03:48,110 --> 00:03:49,780 And then if we go to strings.xml, 72 00:03:49,780 --> 00:03:53,650 we can see we have a new extracted string resource, cool. 73 00:03:53,650 --> 00:03:56,860 So as a challenge for you, go through the rest of your layout files, and 74 00:03:56,860 --> 00:04:00,630 extract all the other warnings into new string resources. 75 00:04:00,630 --> 00:04:02,890 Okay so we're off to a great start with this app. 76 00:04:02,890 --> 00:04:04,700 We've introduced multiple activities, and 77 00:04:04,700 --> 00:04:07,884 now we know how to pass data from one to another using an intent. 78 00:04:07,884 --> 00:04:11,404 Next step we will build the story part of our app following a really common software 79 00:04:11,404 --> 00:04:15,874 design pattern, known as Model View Presenter or MVP for short. 80 00:04:15,874 --> 00:04:18,054 It's pretty awesome, so make sure you're ready for 81 00:04:18,054 --> 00:04:19,834 the fun before going on the next section.