1 00:00:04,720 --> 00:00:07,120 Alright, so in the last video we ended up 2 00:00:07,120 --> 00:00:10,210 by changing the userInput and button 3 00:00:10,210 --> 00:00:13,059 variables to vals instead of vars, and 4 00:00:13,059 --> 00:00:14,860 consequently we were able to remove the 5 00:00:14,860 --> 00:00:17,260 question marks and that's because they're 6 00:00:17,260 --> 00:00:18,880 no longer needed, no longer needing 7 00:00:18,880 --> 00:00:21,220 to be writable. So we can access the 8 00:00:21,220 --> 00:00:22,600 properties then without using question 9 00:00:22,600 --> 00:00:24,580 marks, and you can see that we did that 10 00:00:24,580 --> 00:00:28,630 on lines 18 and 19, and then we 11 00:00:28,630 --> 00:00:32,110 also made a change on line 26, with the user 12 00:00:32,110 --> 00:00:33,280 Input.txt, 13 00:00:33,280 --> 00:00:37,659 dot text. Okay, so what I've done there 14 00:00:37,659 --> 00:00:40,690 is made both userInput and button local 15 00:00:40,690 --> 00:00:44,050 variables in the onCreate function, and 16 00:00:44,050 --> 00:00:44,920 that means that they're not available 17 00:00:44,920 --> 00:00:47,469 anywhere else in the class, only inside 18 00:00:47,469 --> 00:00:50,289 the onCreate function. We've left 19 00:00:50,289 --> 00:00:52,600 TextView as a property, declared up at 20 00:00:52,600 --> 00:00:54,100 the start of the class, so that it will 21 00:00:54,100 --> 00:00:56,350 be available throughout the class. We're 22 00:00:56,350 --> 00:00:57,850 going to see why soon, and that will also 23 00:00:57,850 --> 00:00:59,019 explain why you might want to have 24 00:00:59,019 --> 00:01:02,469 widget references as properties. So it's 25 00:01:02,469 --> 00:01:04,540 worth actually running the program again 26 00:01:04,540 --> 00:01:05,770 just to make sure that we haven't broken 27 00:01:05,770 --> 00:01:09,850 anything, so let's quickly do that. I'm 28 00:01:09,850 --> 00:01:10,660 just going to click on the button 29 00:01:10,660 --> 00:01:12,010 without changing the name, you can see 30 00:01:12,010 --> 00:01:14,230 that's still being appended, so that's 31 00:01:14,230 --> 00:01:17,200 all working fine still. Now there's one 32 00:01:17,200 --> 00:01:19,150 more change I want to make. When the 33 00:01:19,150 --> 00:01:19,960 button's clicked, 34 00:01:19,960 --> 00:01:22,150 you have to backspace it to remove the 35 00:01:22,150 --> 00:01:23,650 text in the editText. So I'm just going 36 00:01:23,650 --> 00:01:24,910 back and having a look there. So if we wanted to 37 00:01:24,910 --> 00:01:26,830 change this, at the moment, we've got to 38 00:01:26,830 --> 00:01:29,050 go back and change it before we can 39 00:01:29,050 --> 00:01:30,550 actually change the text to make it 40 00:01:30,550 --> 00:01:33,190 something different. So what we should 41 00:01:33,190 --> 00:01:34,750 really be doing is clearing the contents 42 00:01:34,750 --> 00:01:36,610 of the editText when the button is 43 00:01:36,610 --> 00:01:39,910 clicked. Now an editText widget's text 44 00:01:39,910 --> 00:01:42,520 property isn't a string, it's an editable. 45 00:01:42,520 --> 00:01:44,260 Now we saw that in the previous video 46 00:01:44,260 --> 00:01:47,200 when using the ctrl key, or command on a 47 00:01:47,200 --> 00:01:49,180 Mac, and hovering over the userInput's 48 00:01:49,180 --> 00:01:51,220 text property. So that means that this 49 00:01:51,220 --> 00:01:53,920 isn't quite as easy as it may appear, but 50 00:01:53,920 --> 00:01:56,920 ultimately, what we need to do is to set 51 00:01:56,920 --> 00:01:59,140 out editText widget's text property to 52 00:01:59,140 --> 00:02:01,440 an empty string, when the button's tapped. 53 00:02:01,440 --> 00:02:03,550 Now if we go back and look at the code, 54 00:02:03,550 --> 00:02:06,550 we were able to set the TextView widget's 55 00:02:06,550 --> 00:02:08,860 text property on line 21, 56 00:02:08,860 --> 00:02:11,230 you can see that here. So we used .text 57 00:02:11,230 --> 00:02:13,540 and set that equal to two double quotes, 58 00:02:13,540 --> 00:02:15,640 which is an empty string. We were able to 59 00:02:15,640 --> 00:02:17,170 do that directly because a string can be 60 00:02:17,170 --> 00:02:18,520 assigned to a TextView 61 00:02:18,520 --> 00:02:21,040 widget's text property. However, the same 62 00:02:21,040 --> 00:02:23,650 can't be done with an editText widget. 63 00:02:23,650 --> 00:02:26,470 So how do we go about solving this? Well 64 00:02:26,470 --> 00:02:28,060 there's actually two ways to do this, and 65 00:02:28,060 --> 00:02:30,310 the change actually goes in the onClick 66 00:02:30,310 --> 00:02:31,660 function, as that's the code that 67 00:02:31,660 --> 00:02:33,670 executes when the user taps the button. 68 00:02:33,670 --> 00:02:36,100 So we're going to come down here, after 69 00:02:36,100 --> 00:02:38,590 doing the append, and we can type user 70 00:02:38,590 --> 00:02:46,380 Input.text.clear parentheses. 71 00:02:46,380 --> 00:02:48,460 So you can see there that I'm using the 72 00:02:48,460 --> 00:02:51,040 clear function of the editable to clear 73 00:02:51,040 --> 00:02:54,280 its contents. Now we can also use the set 74 00:02:54,280 --> 00:02:56,560 Text method, instead of letting Kotlin 75 00:02:56,560 --> 00:02:59,440 access the text property directly. So we 76 00:02:59,440 --> 00:03:00,790 can come back here instead and comment 77 00:03:00,790 --> 00:03:04,690 that line out, and I can instead, do 78 00:03:04,690 --> 00:03:09,790 userInput.setText parentheses and 79 00:03:09,790 --> 00:03:12,010 then two double quotes, which is an empty 80 00:03:12,010 --> 00:03:16,000 string. Now if I use the ctrl command key, 81 00:03:16,000 --> 00:03:17,440 depending on your operating system, to 82 00:03:17,440 --> 00:03:20,170 hover over setText, you can see that it 83 00:03:20,170 --> 00:03:23,170 takes a char sequence type there. So the 84 00:03:23,170 --> 00:03:24,910 string type is compatible with Char 85 00:03:24,910 --> 00:03:27,400 Sequence, so therefore a setText works 86 00:03:27,400 --> 00:03:30,370 as we'd expected it to. Now there's very 87 00:03:30,370 --> 00:03:32,709 little work to choose between these two 88 00:03:32,709 --> 00:03:34,720 approaches, and so you can experiment by 89 00:03:34,720 --> 00:03:36,910 commenting out each one in turn. When you 90 00:03:36,910 --> 00:03:38,590 run the app, you'll see that they both 91 00:03:38,590 --> 00:03:40,959 produce the behavior we want, and just to 92 00:03:40,959 --> 00:03:42,130 confirm that I'm just going to run it 93 00:03:42,130 --> 00:03:44,110 for this one with using the dot 94 00:03:44,110 --> 00:03:52,810 setText. So I can press the button, noting that 95 00:03:52,810 --> 00:03:54,130 the button's cleared now and I can type 96 00:03:54,130 --> 00:03:56,740 Tim, press the button. 97 00:03:56,740 --> 00:03:58,240 That's clearing each time, as you can see. 98 00:03:58,240 --> 00:04:02,080 Alright, time for a mini-challenge. So your 99 00:04:02,080 --> 00:04:03,630 challenge is that when the app starts, 100 00:04:03,630 --> 00:04:06,070 the userInput field starts off 101 00:04:06,070 --> 00:04:09,040 containing the text Name. So the user has 102 00:04:09,040 --> 00:04:10,780 to backspace, as you saw me do, to delete 103 00:04:10,780 --> 00:04:12,850 that before typing in the text they 104 00:04:12,850 --> 00:04:15,370 really want. So this mini-challenge is to 105 00:04:15,370 --> 00:04:17,680 get you to figure out how to remove that 106 00:04:17,680 --> 00:04:19,839 default from the user input widget when 107 00:04:19,839 --> 00:04:22,870 the app first starts. So go ahead and see 108 00:04:22,870 --> 00:04:23,800 if you can figure that out. 109 00:04:23,800 --> 00:04:25,540 Pause the video and come back when 110 00:04:25,540 --> 00:04:28,980 you're ready to see the solution. 111 00:04:28,980 --> 00:04:30,930 Alright, well there's several ways to 112 00:04:30,930 --> 00:04:33,060 do this when programming. So the test 113 00:04:33,060 --> 00:04:35,130 here, again, is does it work, and not that 114 00:04:35,130 --> 00:04:36,660 you got exactly the same code as I'm 115 00:04:36,660 --> 00:04:38,640 about to show you. So you could, for 116 00:04:38,640 --> 00:04:40,290 example, have changed the layout so 117 00:04:40,290 --> 00:04:42,210 there's no text in the editText widget. 118 00:04:42,210 --> 00:04:44,220 That's a good solution and later in the 119 00:04:44,220 --> 00:04:46,350 course, I'll be showing you a way to still 120 00:04:46,350 --> 00:04:48,060 have text in a widget, without it 121 00:04:48,060 --> 00:04:50,370 appearing when the app runs. For my 122 00:04:50,370 --> 00:04:51,690 solution though, I'm going to clear the 123 00:04:51,690 --> 00:04:54,690 text from userInput in the onCreate 124 00:04:54,690 --> 00:04:57,180 method. Now you can also have used the 125 00:04:57,180 --> 00:05:00,840 Clear method, as we saw earlier, so I'm 126 00:05:00,840 --> 00:05:04,140 going to come up here. We've got the line 127 00:05:04,140 --> 00:05:06,510 here, textView dot movementMethod equals 128 00:05:06,510 --> 00:05:09,300 ScrollMovingMethod. After that, I'm 129 00:05:09,300 --> 00:05:11,820 going to call userInput, or type in I 130 00:05:11,820 --> 00:05:17,250 should say, userInput.setText and 131 00:05:17,250 --> 00:05:19,140 double quotes in there. That's going to 132 00:05:19,140 --> 00:05:23,040 be executed after that's been set up. Now 133 00:05:23,040 --> 00:05:25,260 in the next video, I'm going to start by 134 00:05:25,260 --> 00:05:27,330 discussing the activity lifecycle and 135 00:05:27,330 --> 00:05:29,010 show you what went on with our edit 136 00:05:29,010 --> 00:05:31,800 text when the device was rotated. So 137 00:05:31,800 --> 00:05:38,310 let's actually try this out, and we can 138 00:05:38,310 --> 00:05:39,690 see now that we've started out with no 139 00:05:39,690 --> 00:05:42,300 text in the editText, which is correct, 140 00:05:42,300 --> 00:05:44,670 and I can type in something, and it's still 141 00:05:44,670 --> 00:05:47,430 being cleared each time. So that's 142 00:05:47,430 --> 00:05:49,520 working. 143 00:05:49,520 --> 00:05:51,380 Alright, so in the next video what I'm 144 00:05:51,380 --> 00:05:53,060 going to do is discuss the activity 145 00:05:53,060 --> 00:05:55,490 lifecycle and show you what actually 146 00:05:55,490 --> 00:05:57,080 went on with our editext when the 147 00:05:57,080 --> 00:05:59,270 device was rotated, which you saw a few 148 00:05:59,270 --> 00:06:01,910 videos ago. Now by the way, you might find 149 00:06:01,910 --> 00:06:03,410 I talk about methods rather than 150 00:06:03,410 --> 00:06:05,240 functions in some of the videos coming 151 00:06:05,240 --> 00:06:08,210 up. As Kotlin programmers, we're used to 152 00:06:08,210 --> 00:06:10,580 talking about functions. Now in Java, 153 00:06:10,580 --> 00:06:13,099 they're called methods, but don't worry 154 00:06:13,099 --> 00:06:14,330 about the different names. They're 155 00:06:14,330 --> 00:06:16,430 basically the same thing. Because the 156 00:06:16,430 --> 00:06:18,169 lifecycle functions in the Android 157 00:06:18,169 --> 00:06:20,650 framework are currently written in Java, 158 00:06:20,650 --> 00:06:23,509 technically, they are methods rather than 159 00:06:23,509 --> 00:06:25,639 functions. So I generally will use the 160 00:06:25,639 --> 00:06:27,259 word method when talking about framework 161 00:06:27,259 --> 00:06:29,539 functions. And sometimes I may get 162 00:06:29,539 --> 00:06:30,889 confused and say method when I should 163 00:06:30,889 --> 00:06:32,900 say function, and I think I've already 164 00:06:32,900 --> 00:06:34,639 done that a few times in this course, or 165 00:06:34,639 --> 00:06:36,949 vice-versa. But hopefully I won't do it 166 00:06:36,949 --> 00:06:38,360 anymore but if I do, just keep in mind 167 00:06:38,360 --> 00:06:39,800 that they're really the same thing here. 168 00:06:39,800 --> 00:06:41,720 Alright, so what I'm going to do is end 169 00:06:41,720 --> 00:06:43,639 by reformatting the code. So I'm going to 170 00:06:43,639 --> 00:06:48,979 go ahead and do that, Code, Reformat. Al 171 00:06:48,979 --> 00:06:52,659 right, so I'll see you in the next video.