Adding a TextView3:55 with Ben Deitch
We just saw how to edit an existing TextView. Now let's see how to add a new one!
Okay, that first text view was an easy one. 0:00 It was already on the screen. 0:02 Now let's see how we can add another text view to our layout. 0:04 Let's go back to the design view. 0:08 On the left side of the design view, we find the palate. 0:11 This has all sorts of Android controls that we can drag and drop into the layout. 0:14 Inside the palette, components are split into groups to make them easier to find. 0:21 For example the widgets group contains some commonly used elements like buttons, 0:25 check boxes, and progress bars. 0:31 Everything here in the palette is a type of Android view. 0:34 Now a view in the Android sense is a rectangular area on the screen 0:38 that we can see and interact with. 0:42 These items in the palette are all specialized types of views, 0:45 kind of like how a motorcycle is a specialized type of vehicle. 0:49 Inside the text section, find the view called TextView, and then click and 0:53 drag it onto the screen. 0:56 Moving the text view around, we see blue arrows, 1:00 dotted lines, and borders that change depending on where we move it. 1:03 This is because the text view will be placed relative to these 1:08 different areas on the screen. 1:11 We want ours to be centered vertically, but on the left hand side. 1:14 Let's move it until we see a horizontal dotted line through the middle, 1:17 which means we are centered vertically. 1:21 And a green line on the left, which means we are aligned to the left edge. 1:22 Let go of the mass button to drop it in, and there we go. 1:28 Now lets change the text again just like we did before. 1:31 Make sure the text is selected, and then over in on properties pane lets 1:35 delete this text and add a default fact to start with. 1:40 Let's say Ants stretch, 1:44 When they wake up in the morning, and hit Enter. 1:49 Our text seems to be a little small for the screen, but 1:57 we can fix that by changing the text size property. 2:00 Over in the property pane, let's scroll down and look for the text size property. 2:03 Once we've found it, let's set it to 24sp and it Enter. 2:12 Perfect, now we can actually read our fun fact. 2:18 The SP at the end stands for scale independent pixels, which is a relative 2:21 unit that helps your text look the same on devices with different screen sizes and 2:26 different pixel densities, like regular versus HD screens. 2:30 One of the nice things about SP values and why we use them for texts, 2:35 is that they also take into consideration a users preferences for font sizes. 2:39 A user who has a system wide preference for a larger font, 2:44 will have that preference reflected in our app. 2:47 Now since we added a text view here in the design view, 2:50 do you think it added anything in the XML view. 2:53 Sure enough, we've got our new text view. 2:57 And down here at the bottom, we have a new row called android:textSize. 3:00 And it's set to the value we entered. 3:05 We can also see that the text property of this text view 3:07 is set to the fun fact we entered as well. 3:10 Let's get back to the design view. 3:15 And now that we know how to change the text size, pause me for a moment and 3:19 change the text size of the did you know text view to 24 SP. 3:23 Okay, did you pause and do it? 3:30 Last chance, all right, my turn. 3:33 I'll select the TextView, find the textSize property, 3:35 type 24sp, and hit Enter. 3:41 Great job, see if you can figure out how to drag a button onto the screen and 3:45 center it horizontally along the bottom. 3:49 I'll show you how I do it in the next video. 3:52
You need to sign up for Treehouse in order to download course files.Sign up