1 00:00:04,720 --> 00:00:06,970 So now that we've got the layout change 2 00:00:06,970 --> 00:00:09,549 to include our extra button, it's time to 3 00:00:09,549 --> 00:00:11,260 write the code to make the button work. 4 00:00:11,260 --> 00:00:13,809 Now remember that the solution I'm going 5 00:00:13,809 --> 00:00:15,940 to show you is only one solution, so if 6 00:00:15,940 --> 00:00:17,710 your solution works then that's fine 7 00:00:17,710 --> 00:00:20,110 and you've completed the challenge. So 8 00:00:20,110 --> 00:00:21,730 what I'm going to do first is start by 9 00:00:21,730 --> 00:00:23,530 deleting all the commented out code from 10 00:00:23,530 --> 00:00:25,540 a few videos ago. So I'm going to open my 11 00:00:25,540 --> 00:00:27,760 project pane. I'm going to come down to 12 00:00:27,760 --> 00:00:29,800 our MainActivity, and I'm going to 13 00:00:29,800 --> 00:00:31,450 delete all this code, as I mentioned, 14 00:00:31,450 --> 00:00:33,550 that has been commented out. Now I don't 15 00:00:33,550 --> 00:00:36,280 need to delete the imports, but depending 16 00:00:36,280 --> 00:00:38,499 how you've configured Android Studio, you 17 00:00:38,499 --> 00:00:40,659 might have two import lines here, that are 18 00:00:40,659 --> 00:00:42,579 now showing as unused - are sort of greyed 19 00:00:42,579 --> 00:00:44,829 out. So if that's the case, they've probably 20 00:00:44,829 --> 00:00:46,569 been commented out if you've followed, been 21 00:00:46,569 --> 00:00:48,460 following a previous video, so delete 22 00:00:48,460 --> 00:00:50,710 those now. But in my case, because I've 23 00:00:50,710 --> 00:00:52,030 configured Android Studio to 24 00:00:52,030 --> 00:00:54,460 automatically remove unused imports, I 25 00:00:54,460 --> 00:00:56,409 don't need to do anything there. But I'm 26 00:00:56,409 --> 00:00:57,850 going to go ahead and delete this block 27 00:00:57,850 --> 00:00:59,499 over here, that's been, that I've commented out; 28 00:00:59,499 --> 00:01:03,370 the three private definitions Then also 29 00:01:03,370 --> 00:01:05,830 down here, this block of code, which we 30 00:01:05,830 --> 00:01:07,690 discussed the reasons why we don't need 31 00:01:07,690 --> 00:01:09,610 that anymore in the previous video, a few 32 00:01:09,610 --> 00:01:12,040 videos ago. I'm gonna delete that as well, 33 00:01:12,040 --> 00:01:15,670 and just clear up some empty lines there 34 00:01:15,670 --> 00:01:18,460 as well. Alright, so you may have decided 35 00:01:18,460 --> 00:01:20,110 to change the listener for the operator 36 00:01:20,110 --> 00:01:22,420 button, and include the Neg button as 37 00:01:22,420 --> 00:01:24,850 just another operator. For my solution, 38 00:01:24,850 --> 00:01:26,320 though, I decided to create a new 39 00:01:26,320 --> 00:01:28,930 listener, in the same way as we used in 40 00:01:28,930 --> 00:01:31,150 the last section. So what I'm going to do 41 00:01:31,150 --> 00:01:33,250 is add that to the end of the onCreate 42 00:01:33,250 --> 00:01:37,050 method, right down here down the bottom - 43 00:01:37,050 --> 00:01:40,480 basically, here on line 58. So I'm going 44 00:01:40,480 --> 00:01:45,940 to type buttonNeg.set 45 00:01:45,940 --> 00:01:47,010 OnClickListener, 46 00:01:47,010 --> 00:01:50,740 and setOnClickListener 47 00:01:50,740 --> 00:01:53,470 needs the code. So we're going to start 48 00:01:53,470 --> 00:01:58,620 with a left curly brace view and an arrow. 49 00:01:58,620 --> 00:02:02,890 Then on the next line, val value is equal 50 00:02:02,890 --> 00:02:08,639 to a newNumber.text.toString. 51 00:02:08,639 --> 00:02:10,720 Then we're going to do a test here to see 52 00:02:10,720 --> 00:02:15,550 whether it's empty, so if value dot 53 00:02:15,550 --> 00:02:16,870 isEmpty. 54 00:02:16,870 --> 00:02:19,660 If it is empty, then we're going to open a code 55 00:02:19,660 --> 00:02:21,670 block and we're going to set the new 56 00:02:21,670 --> 00:02:25,569 Number, or set number, the edittext, dot 57 00:02:25,569 --> 00:02:28,690 setText parentheses, and we're going to 58 00:02:28,690 --> 00:02:30,430 set that to the minus sign - the 59 00:02:30,430 --> 00:02:33,910 negative sign. Otherwise, we're going to 60 00:02:33,910 --> 00:02:35,799 do some more processing, so we're gonna 61 00:02:35,799 --> 00:02:37,450 have an else block there. And we're going to 62 00:02:37,450 --> 00:02:40,780 start the else block with a try, and 63 00:02:40,780 --> 00:02:45,069 we're going to catch e : and it's going 64 00:02:45,069 --> 00:02:48,340 to be a NumberFormatException, Number 65 00:02:48,340 --> 00:02:52,599 FormatException. And what we're trying 66 00:02:52,599 --> 00:02:54,760 to do here is, we're going to put a 67 00:02:54,760 --> 00:02:55,959 comment here, indicating what would have 68 00:02:55,959 --> 00:02:57,430 happened for a Number Format Exception. 69 00:02:57,430 --> 00:03:00,420 So in in this case, the new number was 70 00:03:00,420 --> 00:03:05,019 set to that already, or a period. So clear 71 00:03:05,019 --> 00:03:09,610 it, if that's the case; newNumber.set 72 00:03:09,610 --> 00:03:11,860 Text parentheses and two double quotes, 73 00:03:11,860 --> 00:03:13,840 just to an empty string. So that's what 74 00:03:13,840 --> 00:03:15,760 happened if the newNumber Format 75 00:03:15,760 --> 00:03:18,549 Exception is thrown, but assuming it's not 76 00:03:18,549 --> 00:03:20,170 thrown, we want to add some code there 77 00:03:20,170 --> 00:03:22,870 to process the number accordingly. So 78 00:03:22,870 --> 00:03:27,040 we're gonna put var doubleValue is 79 00:03:27,040 --> 00:03:31,870 equal to value.toDouble. Then we're 80 00:03:31,870 --> 00:03:35,889 gonna type doubleValue star equals minus 81 00:03:35,889 --> 00:03:37,420 1, so we're multiplying the value by 82 00:03:37,420 --> 00:03:39,840 negative 1. Then we're gonna type newNumber 83 00:03:39,840 --> 00:03:48,000 dot setText doubleValue.toString, 84 00:03:48,000 --> 00:03:50,200 so converting it back to a string so 85 00:03:50,200 --> 00:03:52,780 that we can set the EditText back with 86 00:03:52,780 --> 00:03:55,450 a value - and that should, effectively, be 87 00:03:55,450 --> 00:03:57,400 it. So I've set the onClickListener, 88 00:03:57,400 --> 00:03:59,079 basically, in the same way as we did for 89 00:03:59,079 --> 00:04:01,030 the Button Click Counter, using a lambda. 90 00:04:01,030 --> 00:04:03,579 Now, no other buttons will be using this 91 00:04:03,579 --> 00:04:05,470 listener, so there's no need to create a 92 00:04:05,470 --> 00:04:07,690 named instance, but there's no problem if 93 00:04:07,690 --> 00:04:09,910 you have in your solution. And we'll 94 00:04:09,910 --> 00:04:11,560 probably get a warning from Android 95 00:04:11,560 --> 00:04:13,690 Studio, because one of our layouts 96 00:04:13,690 --> 00:04:16,029 doesn't have a buttonNeg. So if you see 97 00:04:16,029 --> 00:04:17,160 that, don't worry too much about that. 98 00:04:17,160 --> 00:04:22,990 And come over here and have a look, and I've 99 00:04:22,990 --> 00:04:24,880 got a semicolon there from my Java days, so 100 00:04:24,880 --> 00:04:26,169 let me delete that, because it's warning 101 00:04:26,169 --> 00:04:27,909 us about that - which you don't need 102 00:04:27,909 --> 00:04:29,320 because, of course, this is Kotlin and not 103 00:04:29,320 --> 00:04:30,249 Java. 104 00:04:30,249 --> 00:04:32,199 And this is the other one I was talking 105 00:04:32,199 --> 00:04:34,149 about; The resource is missing in some of 106 00:04:34,149 --> 00:04:36,129 layout versions. Parameter view is never 107 00:04:36,129 --> 00:04:37,899 used, and it's talking about the fact here, 108 00:04:37,899 --> 00:04:40,929 that we don't, basically, don't have a 109 00:04:40,929 --> 00:04:43,539 buttonNeg button on both our layouts 110 00:04:43,539 --> 00:04:44,709 because, of course, we've only, at the 111 00:04:44,709 --> 00:04:47,769 moment, updated the landscape version. So 112 00:04:47,769 --> 00:04:50,559 the code itself is pretty simple. If the 113 00:04:50,559 --> 00:04:52,689 text in the widget is empty, then we're 114 00:04:52,689 --> 00:04:54,279 setting it to the minus character and 115 00:04:54,279 --> 00:04:56,529 we're done - that's this code up here, on 116 00:04:56,529 --> 00:04:59,499 line 61. Now the four, the other digit 117 00:04:59,499 --> 00:05:01,119 buttons will add their digits after the 118 00:05:01,119 --> 00:05:03,039 minus, so negative numbers can be 119 00:05:03,039 --> 00:05:05,829 entered. However, if the input text isn't 120 00:05:05,829 --> 00:05:07,779 blank, we go into the else code block. 121 00:05:07,779 --> 00:05:10,209 The code then tries to negate any 122 00:05:10,209 --> 00:05:12,249 number that may have been typed into the 123 00:05:12,249 --> 00:05:14,919 widget. Now if that works, the number is 124 00:05:14,919 --> 00:05:17,019 multiplied by negative 1 to negate it, 125 00:05:17,019 --> 00:05:20,559 here on line 65. Then the widget is 126 00:05:20,559 --> 00:05:22,929 updated with the new value. So positive 127 00:05:22,929 --> 00:05:24,729 numbers in that scenario become negative, 128 00:05:24,729 --> 00:05:27,099 and negative numbers become positive, and 129 00:05:27,099 --> 00:05:29,289 that's exactly what we want here. If 130 00:05:29,289 --> 00:05:31,539 however, there wasn't a valid number, then 131 00:05:31,539 --> 00:05:32,559 that's going to be picked up by the 132 00:05:32,559 --> 00:05:34,599 try/catch block. Now we've already 133 00:05:34,599 --> 00:05:36,669 checked for an empty string, and we're 134 00:05:36,669 --> 00:05:38,019 actually setting the text to a minus 135 00:05:38,019 --> 00:05:40,119 sign if that is the case. So we don't 136 00:05:40,119 --> 00:05:41,259 need to check for that again, in the 137 00:05:41,259 --> 00:05:43,389 catch block. So the only other values 138 00:05:43,389 --> 00:05:44,919 that could cause the catch block to be 139 00:05:44,919 --> 00:05:47,139 entered, are the minus symbol by itself, 140 00:05:47,139 --> 00:05:49,059 if that's been entered, or a decimal 141 00:05:49,059 --> 00:05:52,599 point by itself, or a minus decimal. So 142 00:05:52,599 --> 00:05:53,679 all we really have to do is clear the 143 00:05:53,679 --> 00:05:58,059 text in the widget, in that scenario. So 144 00:05:58,059 --> 00:06:00,189 that's it - the app should still work but 145 00:06:00,189 --> 00:06:02,829 I do have to be careful now, how I test it 146 00:06:02,829 --> 00:06:05,079 at this stage though. 147 00:06:05,079 --> 00:06:07,629 Remember that we haven't modified the 148 00:06:07,629 --> 00:06:10,509 portrait layout yet, so the layout 149 00:06:10,509 --> 00:06:12,610 won't have a buttonNeg, and the code 150 00:06:12,610 --> 00:06:14,669 will crash if I run it in portrait mode. So 151 00:06:14,669 --> 00:06:18,459 I'm going to start the emulator up. I'm 152 00:06:18,459 --> 00:06:21,129 going to run the app, and I'm going to 153 00:06:21,129 --> 00:06:22,629 switch to landscape before doing 154 00:06:22,629 --> 00:06:23,769 anything else, just to make sure it 155 00:06:23,769 --> 00:06:24,999 doesn't crash - and that's probably too 156 00:06:24,999 --> 00:06:27,999 slow to do that now, but I'll try. So I'll 157 00:06:27,999 --> 00:06:29,739 switch to landscape, basically, to avoid 158 00:06:29,739 --> 00:06:31,329 the portrait code from running, and 159 00:06:31,329 --> 00:06:33,759 luckily the program is still compiling, 160 00:06:33,759 --> 00:06:40,190 so I'll just wait and speed this up. 161 00:06:40,190 --> 00:06:42,150 Okay, so you can see the app's running. 162 00:06:42,150 --> 00:06:43,620 Because we're in landscape mode, we 163 00:06:43,620 --> 00:06:45,510 didn't crash, and we can see our neg 164 00:06:45,510 --> 00:06:48,660 Button over here. So let's see whether it 165 00:06:48,660 --> 00:06:52,830 works. I'm going to start by typing 58 x 166 00:06:52,830 --> 00:07:06,030 9, pressing neg, + 22 / neg, and the 167 00:07:06,030 --> 00:07:12,510 negative sign there, 5 equals. So 168 00:07:12,510 --> 00:07:14,100 basically, we end up with the value of 169 00:07:14,100 --> 00:07:17,430 100, which is correct in this case. So 170 00:07:17,430 --> 00:07:18,990 basically, you saw that it successfully 171 00:07:18,990 --> 00:07:21,390 negates an existing number 9, and also 172 00:07:21,390 --> 00:07:23,670 allowed for a negative number - negative 5 - 173 00:07:23,670 --> 00:07:25,830 to be entered. Now of course, if I go trying 174 00:07:25,830 --> 00:07:28,650 to go back now to portrait mode, we're 175 00:07:28,650 --> 00:07:30,030 going to get a crash, as we see straight 176 00:07:30,030 --> 00:07:31,440 away, and it said, actually, a 177 00:07:31,440 --> 00:07:32,880 NullPointerException, if we have a look 178 00:07:32,880 --> 00:07:40,100 in logcat; a NullPointerException, 179 00:07:40,100 --> 00:07:42,390 attempting to evoke virtual method 180 00:07:42,390 --> 00:07:43,830 for the Button.set 181 00:07:43,830 --> 00:07:44,550 OnClickListener. 182 00:07:44,550 --> 00:07:47,760 And that is, of course, because we haven't 183 00:07:47,760 --> 00:07:49,830 added the button to the portrait layout 184 00:07:49,830 --> 00:07:52,650 yet. Now sometimes you have to create 185 00:07:52,650 --> 00:07:54,780 different layouts, so that your app looks 186 00:07:54,780 --> 00:07:57,300 ok at different screen sizes. Doing that 187 00:07:57,300 --> 00:07:59,160 does create extra work because you have 188 00:07:59,160 --> 00:08:01,950 to maintain more than one layout. Now you 189 00:08:01,950 --> 00:08:03,510 can test the app before modifying all 190 00:08:03,510 --> 00:08:05,760 the layouts, just like I did here, to make 191 00:08:05,760 --> 00:08:07,680 sure it all works, before putting in the 192 00:08:07,680 --> 00:08:10,950 effort to modify both layouts. Just be 193 00:08:10,950 --> 00:08:12,570 careful to set your emulator into the 194 00:08:12,570 --> 00:08:14,580 working mode before running the app, and 195 00:08:14,580 --> 00:08:16,500 don't cause the other layout to be used, 196 00:08:16,500 --> 00:08:18,000 until you've gone ahead and created, or 197 00:08:18,000 --> 00:08:20,250 modified it. Alright, so I'll end this 198 00:08:20,250 --> 00:08:21,840 video here. In the next one, we'll make 199 00:08:21,840 --> 00:08:24,090 changes to the portrait layout, and add 200 00:08:24,090 --> 00:08:25,680 the negButton to that portrait layout 201 00:08:25,680 --> 00:08:27,810 to get it working, and we'll then have the 202 00:08:27,810 --> 00:08:31,790 app completed. See you in the next video.