Adding Story Resources2:57 with Ben Jakuben
Our story consists mainly of text and images. We recently learned how to best handle text in an app via string resources, so let's see how to use them for our story.
Code for copy/paste
<string name="page0">On your return trip from studying Saturn\'s rings, you hear a distress signal that seems to be coming from the surface of Mars. It\'s strange because there hasn\'t been a colony there in years. Even stranger, it\'s calling you by name: \"Help me, %1$s, you\'re my only hope.\"</string> <string name="page0_choice1">Stop and investigate</string> <string name="page0_choice2">Continue home to Earth</string> <string name="page1">You deftly land your ship near where the distress signal originated. You didn\'t notice anything strange on your fly-by, but there is a cave in front of you. Behind you is an abandoned rover from the early 21st century.</string> <string name="page1_choice1">Explore the cave</string> <string name="page1_choice2">Explore the rover</string> <string name="page2">You continue your course to Earth. Two days later, you receive a transmission from HQ saying that they have detected some sort of anomaly on the surface of Mars near an abandoned rover. They ask you to investigate, but ultimately the decision is yours because your mission has already run much longer than planned and supplies are low.</string> <string name="page2_choice1">Head back to Mars to investigate</string> <string name="page2_choice2">Continue home to Earth</string> <string name="page3">Your EVA suit is equipped with a headlamp, which you use to navigate the cave. After searching for a while your oxygen levels are starting to get pretty low. You know you should go refill your tank, but there\'s a very faint light up ahead.</string> <string name="page3_choice1">Refill at ship and explore the rover</string> <string name="page3_choice2">Continue towards the faint light</string> <string name="page4">The rover is covered in dust and most of the solar panels are broken. But you are quite surprised to see the on-board system booted up and running. In fact, there is a message on the screen: \"%1$s, come to 28.543436, -81.369031.\" Those coordinates aren\'t far, but you don\'t know if your oxygen will last for a trip there and back.</string> <string name="page4_choice1">Explore the coordinates</string> <string name="page4_choice2">Return to Earth</string> <string name="page5">After a long walk slightly uphill, you end up at the top of a small crater. You look around, and are overjoyed to see your favorite android, %1$s-S1124. It had been lost on a previous mission to Mars! You take it back to your ship and fly back to Earth.</string> <string name="page6">You arrive home on Earth. While your mission was a success, you forever wonder what was sending that signal. Perhaps a future mission will be able to investigate…</string>
We have so much text in this story. 0:00 So many strings, which is why we should put it all in our string resource file. 0:03 So, let's open up strings.xml again. 0:07 And we can start here at the bottom by adding a new line and typing string name=. 0:10 And let's call this page0. 0:17 We could name our pages anything we want, but when I wrote this story I mapped it 0:21 out like an array that starts at 0, because that's how I was thinking of it. 0:24 We could call this page something like start_page instead. 0:29 Really, what we want to do is pick a consistent way to name things 0:33 that makes sense of the structure. 0:36 Let's stick with a page followed by a number, 0:38 which matches up with our finite state machine diagram. 0:40 Okay, so next I will paste in the text of the page right here. 0:44 This text and all of the other strings we will add are available for 0:47 you in the teacher's notes. 0:51 Okay, let's take a look at this string. 0:52 First, let's change it so that the text wraps on the screen. 0:54 If we click on View and then go down to Active Editor here. 0:57 We can click on Use Soft Wraps. 1:00 That makes it easier then trying to scroll around. 1:03 So in the string, notice that I had to add a lot of backward slash escape characters. 1:06 There are a lot of special characters, especially that have to do with 1:12 punctuation like these single quotes and double quotes. 1:14 And to put them in our string resource file, 1:18 we need to use the backwards slash to escape them. 1:20 Here you can see what happens if we forget the backwards slash, 1:24 we should get a warning. 1:26 There you go. It pops up. 1:28 And if we hover over it, it says Apostrophe not preceded by \. 1:29 So, it's pretty obvious to see what the error is and how to fix it. 1:33 There's also a special sequence here at the end, 1:36 where we will plug in the user's name. 1:39 This is a string formatting sequence that we will learn more about shortly. 1:41 So next, we should add the choice labels for this first page. 1:45 I'm going to add a new string. 1:49 It's name will be page0_choice1. 1:50 And the first button will say Stop and Investigate. 1:57 Then we'll do the same thing, string name="page0_choice2". 2:04 And this button will say Continue home to Earth. 2:11 Okay, rather than talk through the rest of the pages, 2:16 which all have this same format, let's do some more copying and pasting. 2:18 So, this code I'm pasting is available for you to copy from the teacher's notes so 2:22 that you don't have to type it either. 2:27 I'm gonna add a few spaces at the bottom and 2:29 make sure everything has enough room on the screen. 2:31 And essentially what we've done is put the text for each page into a group like this. 2:33 It doesn't matter that they're grouped together or that they have spaces between 2:38 them, I just did that so I would know how to find page 0, page 1, etc. 2:42 All right, so all of our text in our story is now stored in this file. 2:45 This is a convenient place for us to manage the text, but 2:49 remember that it's also useful if we ever want to add additional string files for 2:52 other languages. 2:56
You need to sign up for Treehouse in order to download course files.Sign up