1 00:00:00,810 --> 00:00:05,640 ‫-: Hi. Within this lecture we are going to connect our views 2 00:00:05,640 --> 00:00:07,740 ‫with our Java code. 3 00:00:07,740 --> 00:00:08,850 ‫So in order to do that 4 00:00:08,850 --> 00:00:12,150 ‫I'm going to go over to my main activity. 5 00:00:12,150 --> 00:00:15,120 ‫Okay? We are done with the user interface right now 6 00:00:15,120 --> 00:00:17,580 ‫and we are gonna write some code. 7 00:00:17,580 --> 00:00:20,939 ‫So first of all, it's very good idea to start 8 00:00:20,939 --> 00:00:24,870 ‫with writing the methods that we have defined 9 00:00:24,870 --> 00:00:28,260 ‫as on click methods in our xml 10 00:00:28,260 --> 00:00:30,900 ‫so that we don't forget them, okay? 11 00:00:30,900 --> 00:00:33,420 ‫I'm not writing it under on create 12 00:00:33,420 --> 00:00:38,250 ‫but rather under my main activity I'm outside of on create 13 00:00:38,250 --> 00:00:42,510 ‫I'm gonna say public void and I'm going to start with sum. 14 00:00:42,510 --> 00:00:45,360 ‫So this is how we create methods, remember 15 00:00:45,360 --> 00:00:49,890 ‫but we have to add, view view parameter over here 16 00:00:49,890 --> 00:00:54,840 ‫in order to specify this method is going to be called 17 00:00:54,840 --> 00:00:56,220 ‫by a view. 18 00:00:56,220 --> 00:00:57,660 ‫So if you get a error like this 19 00:00:57,660 --> 00:01:00,600 ‫just click over here and hit, hit alt enter 20 00:01:00,600 --> 00:01:05,130 ‫in your keyboard and as you can see it's imported over here 21 00:01:05,130 --> 00:01:09,240 ‫like import view and then say view. 22 00:01:09,240 --> 00:01:12,840 ‫So we are doing this in order to let us know 23 00:01:12,840 --> 00:01:16,650 ‫that this method will be get called by a view 24 00:01:16,650 --> 00:01:19,320 ‫like a button or image view. 25 00:01:19,320 --> 00:01:21,990 ‫So I'm going to continue doing that. 26 00:01:21,990 --> 00:01:26,990 ‫I believe we have deduct, view view, okay, and we are done. 27 00:01:28,020 --> 00:01:30,120 ‫Let's go for multiply. 28 00:01:30,120 --> 00:01:34,200 ‫So public void multiply, view, view. 29 00:01:34,200 --> 00:01:37,200 ‫And then for the last one we are gonna go 30 00:01:37,200 --> 00:01:40,710 ‫for divide public void divide. 31 00:01:40,710 --> 00:01:42,000 ‫And here you go. 32 00:01:42,000 --> 00:01:46,140 ‫Now we are done with all the public methods over here 33 00:01:46,140 --> 00:01:49,140 ‫in order to be sure 34 00:01:49,140 --> 00:01:52,593 ‫that we have the on click methods in our coding section. 35 00:01:53,580 --> 00:01:56,910 ‫So what do we want to do in this app? 36 00:01:56,910 --> 00:02:00,510 ‫We want to get the input of the user right? 37 00:02:00,510 --> 00:02:05,510 ‫We want to get the numbers that our user chooses to give us 38 00:02:06,350 --> 00:02:08,520 ‫and that we are gonna do some kind 39 00:02:08,520 --> 00:02:11,250 ‫of mathematical operation on those numbers 40 00:02:11,250 --> 00:02:16,250 ‫and we are going to display the result on a result text. 41 00:02:16,410 --> 00:02:21,030 ‫So we need our text we use in edit text 42 00:02:21,030 --> 00:02:23,340 ‫inside of our coding section. 43 00:02:23,340 --> 00:02:26,490 ‫We need to connect them first, right? 44 00:02:26,490 --> 00:02:29,460 ‫And maybe you remember how to do this thing. 45 00:02:29,460 --> 00:02:32,040 ‫So I'm gonna do this under on create 46 00:02:32,040 --> 00:02:35,047 ‫and we're gonna talk about why we are doing this 47 00:02:35,047 --> 00:02:35,880 ‫under on create. 48 00:02:35,880 --> 00:02:40,230 ‫So I'm going to start with edit text and here you go. 49 00:02:40,230 --> 00:02:42,030 ‫We have to do alt enter. 50 00:02:42,030 --> 00:02:46,770 ‫So edit text is the type that I'm defining over here 51 00:02:46,770 --> 00:02:48,720 ‫and you can call it whatever you want. 52 00:02:48,720 --> 00:02:51,690 ‫For example, I'm gonna call this number 1 text. 53 00:02:51,690 --> 00:02:55,110 ‫You can call it edit text, you can call it your name. 54 00:02:55,110 --> 00:02:58,590 ‫But after that you have to say find view by Id 55 00:02:58,590 --> 00:03:03,590 ‫and R.id. whatever Id you have given to your edit text. 56 00:03:05,040 --> 00:03:08,880 ‫Like in my case, it's already number 1 text. 57 00:03:08,880 --> 00:03:13,880 ‫So over here I'm choosing the ID that I have given in xml. 58 00:03:14,880 --> 00:03:18,780 ‫Now I'm gonna do the same thing for number 2 text as well. 59 00:03:18,780 --> 00:03:23,780 ‫I'm going to say find view by Id, R.ID dot number 2 text. 60 00:03:25,110 --> 00:03:28,680 ‫And finally we have our text view, right? 61 00:03:28,680 --> 00:03:32,250 ‫So I'm gonna call this result text and say 62 00:03:32,250 --> 00:03:37,230 ‫findview by Id R.id result text. 63 00:03:37,230 --> 00:03:42,210 ‫So beware that these are the same with this 64 00:03:42,210 --> 00:03:43,890 ‫but this is just a coincidence. 65 00:03:43,890 --> 00:03:47,190 ‫You can call this whatever you want, okay? 66 00:03:47,190 --> 00:03:48,870 ‫But this is a convention. 67 00:03:48,870 --> 00:03:50,340 ‫It is fine. 68 00:03:50,340 --> 00:03:54,750 ‫It is a good way to call this things with the 69 00:03:54,750 --> 00:03:57,120 ‫exact functionality that they're 70 00:03:57,120 --> 00:03:59,760 ‫they're going to do in the app. 71 00:03:59,760 --> 00:04:01,627 ‫So you can call this your name as well. 72 00:04:01,627 --> 00:04:05,040 ‫It won't, it won't break, it will work 73 00:04:05,040 --> 00:04:10,040 ‫but it's a good habit to choose good naming. 74 00:04:10,440 --> 00:04:13,920 ‫Okay? So as you can see, 75 00:04:13,920 --> 00:04:16,680 ‫I have all this texts over here like 76 00:04:16,680 --> 00:04:20,070 ‫edit text, edit text two and text view. 77 00:04:20,070 --> 00:04:25,070 ‫Now if I want to get text out of an edit text, I can do it 78 00:04:25,680 --> 00:04:30,680 ‫like this, like number1text.getText, okay? 79 00:04:31,380 --> 00:04:36,000 ‫And then if I say .toString like this 80 00:04:36,000 --> 00:04:40,500 ‫it will give us the string that user has typed 81 00:04:40,500 --> 00:04:42,210 ‫in that edit text. 82 00:04:42,210 --> 00:04:46,784 ‫We are going to discuss why are we adding some two string 83 00:04:46,784 --> 00:04:50,493 ‫or what is the get text and stuff later on. 84 00:04:51,960 --> 00:04:54,360 ‫However, I want you to understand 85 00:04:54,360 --> 00:04:58,297 ‫that we can use this get text .two string methods 86 00:04:58,297 --> 00:05:03,297 ‫in order to get the current string in this edit text. 87 00:05:03,720 --> 00:05:06,990 ‫But there is no point doing that under on create, right? 88 00:05:06,990 --> 00:05:09,810 ‫Because when we create this activity 89 00:05:09,810 --> 00:05:14,130 ‫the edit text will be completely empty like this. 90 00:05:14,130 --> 00:05:17,340 ‫So when a user taps one of these buttons 91 00:05:17,340 --> 00:05:21,480 ‫that's when we try and get the content 92 00:05:21,480 --> 00:05:26,070 ‫of this edit text so that it can actually take these numbers 93 00:05:26,070 --> 00:05:29,670 ‫and add them together and show the result, right? 94 00:05:29,670 --> 00:05:32,340 ‫So we want to do this thing like 95 00:05:32,340 --> 00:05:37,340 ‫number1Text.getText.toString in one of the methods 96 00:05:38,610 --> 00:05:41,640 ‫actually in all of the methods. 97 00:05:41,640 --> 00:05:46,640 ‫So if I write number1Text, can I reach the text, 98 00:05:46,950 --> 00:05:50,910 ‫the variable that I have defined under on create? 99 00:05:50,910 --> 00:05:53,790 ‫Of course I cannot because they are in 100 00:05:53,790 --> 00:05:57,270 ‫different scope right? We have talked about this. 101 00:05:57,270 --> 00:05:59,400 ‫So we have this coding block over here 102 00:05:59,400 --> 00:06:02,520 ‫and this coding block over here, we cannot 103 00:06:02,520 --> 00:06:07,500 ‫actually reach the variable that we have defined over here. 104 00:06:07,500 --> 00:06:09,420 ‫So maybe you can think something like that. 105 00:06:09,420 --> 00:06:12,810 ‫Yeah, let's just copy and paste things over here 106 00:06:12,810 --> 00:06:15,780 ‫and over here and over every method that we have 107 00:06:15,780 --> 00:06:18,330 ‫and it will actually work, 108 00:06:18,330 --> 00:06:20,700 ‫but it's not very efficient, right? 109 00:06:20,700 --> 00:06:23,130 ‫It's not efficient for us because we have to 110 00:06:23,130 --> 00:06:25,470 ‫copy and paste and it's not efficient 111 00:06:25,470 --> 00:06:28,140 ‫for memory consumption as well. 112 00:06:28,140 --> 00:06:31,920 ‫Every time a user taps on one of these buttons 113 00:06:31,920 --> 00:06:36,920 ‫then I will have to create this views from scratch. 114 00:06:37,140 --> 00:06:40,560 ‫So it's kind of inefficient. 115 00:06:40,560 --> 00:06:43,020 ‫So what, what do we do? 116 00:06:43,020 --> 00:06:45,930 ‫Remember we talked about is we can actually 117 00:06:45,930 --> 00:06:50,930 ‫define the variables and also the views inside of our class 118 00:06:51,150 --> 00:06:54,240 ‫but outside all, all the methods so that we can 119 00:06:54,240 --> 00:06:56,459 ‫reach them from anywhere we want, 120 00:06:56,459 --> 00:06:59,250 ‫inside of the class obviously. 121 00:06:59,250 --> 00:07:02,580 ‫For example, we have done this like, int myNumber 122 00:07:02,580 --> 00:07:06,120 ‫so we can reach my number from any method. 123 00:07:06,120 --> 00:07:09,270 ‫We can do the same thing for the edit text 124 00:07:09,270 --> 00:07:12,780 ‫or text view or image view or buttons as well. 125 00:07:12,780 --> 00:07:14,580 ‫For example, I'm gonna copy this 126 00:07:14,580 --> 00:07:18,690 ‫and paste it over here and put a semicolon the end. 127 00:07:18,690 --> 00:07:23,370 ‫Okay, so I'm going to do the same thing over here as well. 128 00:07:23,370 --> 00:07:25,230 ‫Number 2 text 129 00:07:25,230 --> 00:07:29,580 ‫and I'm going to do the same thing over here as well. 130 00:07:29,580 --> 00:07:33,840 ‫So this is defining, okay? 131 00:07:33,840 --> 00:07:38,160 ‫If I delete the types from here like this, 132 00:07:38,160 --> 00:07:41,190 ‫this is initializing. 133 00:07:41,190 --> 00:07:45,390 ‫So right now I'm initializing the views over here 134 00:07:45,390 --> 00:07:48,690 ‫assigning the values to them 135 00:07:48,690 --> 00:07:51,150 ‫and over here I'm defining them. 136 00:07:51,150 --> 00:07:55,560 ‫So right now if I write number 1 text or number 2 text 137 00:07:55,560 --> 00:07:59,760 ‫or result text from anywhere in coding section 138 00:07:59,760 --> 00:08:02,193 ‫then I can actually reach those. 139 00:08:03,060 --> 00:08:07,140 ‫So under on create or under some or under deduct. 140 00:08:07,140 --> 00:08:08,940 ‫And they will be initialized 141 00:08:08,940 --> 00:08:12,240 ‫they will be actually connected 142 00:08:12,240 --> 00:08:16,170 ‫to the related IDs under on create. 143 00:08:16,170 --> 00:08:19,353 ‫So it won't be any problem for me either. 144 00:08:20,640 --> 00:08:24,360 ‫So right now if I come over here and say number 1 145 00:08:24,360 --> 00:08:26,940 ‫as you can see, I can reach it. 146 00:08:26,940 --> 00:08:29,400 ‫So that's good. 147 00:08:29,400 --> 00:08:32,430 ‫Now we have defined all the views 148 00:08:32,430 --> 00:08:34,770 ‫that we are going to be needing 149 00:08:34,770 --> 00:08:37,500 ‫we are going to be using in this app. 150 00:08:37,500 --> 00:08:41,313 ‫Now we can actually move on to the coding section as well.