Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Formatting Strings6:35 with Ben Jakuben
We said that we would plug the reader's name into the story, so let's see how to do it using the String.format() method.
A while ago we talked about taking the name that we get from the user, and 0:00 then plugging it directly into our story. 0:03 Let's do that now. 0:05 Instead of just sending the text in the text view directly from our page object, 0:06 we're going to search for a specific string, replace it with our users name. 0:10 So let's create a new string variable to hold the text of our story. 0:14 String pageText = getString and then we can get it right from string resources. 0:17 And again, from the page object, we want to get the Text ID associated for 0:24 the given page. 0:28 We'll use this page text variable to set the text in our text view. 0:29 But we're going to manipulate it before we pass it in. 0:33 So the code we're gonna write here is some simple Java code that you may be familiar 0:35 with from our Java courses. 0:38 But if not, we'll talk it over briefly. 0:39 Basically it's just a search and replace for that special sequence we saw earlier. 0:41 I'm gonna add a few lines here and bump my code up. 0:45 And now on a new line type pageText equals String with a capital s 0:49 .format, then for the first parameter let's use pageText, 0:54 and for the second parameter let's pass in the name. 0:59 But wait. 1:02 I don't remember if this name is available down here. 1:03 We haven't passed it into our loadPage method. 1:06 And let's see, up here we're setting it, but 1:08 here's where we declare it in the onCreate method. 1:11 Let's make this a member variable instead, that we can access both in here and 1:13 onCreate, and in the loadPage method. 1:17 I'd rather make it a member variable because we're gonna use the same name in 1:19 a couple of different pages. 1:22 So, I'll just delete string, and then up here at the top say private String name. 1:24 And now we can set it here, and use it down here, cool. 1:31 So in this new line of code we are going to format pageText by passing in the name. 1:36 And then we'll store the reformatted result back into pageText. 1:41 So we'll work on it here and then we'll store the result back in this variable. 1:46 Let's take a quick look at the string documentation for 1:49 Java from the Oracle website. 1:51 So here we have the string class. 1:54 And if we scroll down a little bit we can go to the method summary, and 1:55 then here we find two different format methods. 1:59 The first one has a Locale, but 2:02 we just want the second one here that takes a String and Object parameters. 2:03 This format method is a Static method, which is why we call it in a from a String 2:07 class, and not from a specific instance of a String. 2:10 This concept of static versus instance methods maybe a little confusing, 2:13 based on how familiar you are with Java. 2:17 But static methods like this mean that we don't need an actual 2:20 instance of a string to call it, which is why we're able to just use the class name. 2:22 A basic idea here is we can plug in special characters in our string and 2:26 this method will replace those special characters when it finds a match 2:29 with whatever arguments we provide. 2:33 So let's click on the format string here, to see some examples. 2:35 So here, we have a birthday example with some special placeholders. 2:39 Percent one with a dollar sign and a specific code which stands for 2:42 month, then day, and then year in this example. 2:47 So if we go back to our story code, let's go to our string resources and 2:51 I've already plugged in the appropriate characters we need for page zero. 2:55 So I'm going to turn on that soft wrap feature again. 2:59 We click on View, Active Editor, Use Soft Wraps. 3:01 Now, all the text for page0, will wrap on the screen. 3:06 And here you can see %1$s. 3:09 %1 is the order number of the parameter. 3:10 We could have multiple parameters but we only have one, so it's just gonna be %1. 3:15 If we had a second parameter, for example, it would be %2. 3:19 And then the $s, is just for a simple string. 3:22 So this will say, "Help me, user, you're my only hope". 3:26 This same sequence shows up in page5 too, if we scroll down here, 3:29 we see our favorite android, and again, we have %1$s. 3:34 So let's go back to the story activity. 3:38 The cool thing about this method is that if no match is found, 3:40 then the name won't be plugged, which is why we can call this on every single page. 3:43 So let's add a comment just to clarify. 3:47 Add the name if the placeholder is included, and it won't add if it's not. 3:50 What else do we need to do here. 3:59 We need to set the text view with this modified text. 4:01 On the next line type storyTextView.setText, that's the method 4:04 to set this text of course, and we can pass in the newly formatted pageText. 4:10 We also need to set the text for each button. 4:15 That's easy enough. 4:18 On a new line, type choice1Button.setText, and 4:19 then we can pass in the string ID as the parameter here, 4:24 page.getChoice1().getTextId(). 4:27 Notice that setText is overloaded on these widgets. 4:32 So the first one, takes an actual string, 4:37 and the second one here, takes the ID of a string resource. 4:39 Let's do the same thing for choice2Button. 4:43 setText(page.getChoice2().getTextId(), and finish it off. 4:45 Right, let's run this, and we should be able to see all four 4:54 of the elements on our screen with the appropriate page0 text. 4:57 There it loads, I enter my name, start my adventure and 5:00 we've got it all here and check it out there's my name showing up in the story. 5:04 All right. 5:11 We should also verify that our scroll view is working by running this on 5:12 emulator with a smaller screen size. 5:16 So back here in Android studio, we're gonna stop the current process. 5:18 And I am gonna click on AVD Manager. 5:21 I currently don't have an emulator with a smaller screen, so 5:24 I am gonna Create a New Virtual Device, and 5:27 I'll take the Nexus One simply because it has the smallest screen size on my list. 5:29 I'm running the X86 version of the emulator, I'll pick Nougat, and hit Next. 5:34 Finish this, and now what I want to do is start it, and when I run the app again, 5:39 I'll make sure I go into this emulator instead of the Pixel. 5:44 All right my emulator just finished, I'm gonna close this and 5:46 now if I click run I can pick the Nexus One. 5:50 I'm only gonna do this one time, though, so I'm going to uncheck this and 5:53 I'll put it back on the Pixel. 5:56 All right, so this fits okay on the small screen. 5:58 Type my name, start the adventure, and now you can see that the text is running down. 6:02 And if we scroll up we can read the rest of it, and 6:07 sure enough, our name still shows up. 6:10 Cool. All right just like we want it, 6:12 the scroll view is scrolling both the image and 6:13 the text view while the buttons remain in place at the bottom of the screen. 6:16 Very nice. 6:19 All right, so we have successfully loaded some story data into our view and 6:21 we've got the scroll view working just like we wanted to. 6:25 Coming up in the next section, we'll get the rest of the story working, and 6:27 we'll also take a look at how to properly navigate within an Android App. 6:31
You need to sign up for Treehouse in order to download course files.Sign up