‫-: Hi. Within this lecture we are going to connect our views
‫with our Java code.
‫So in order to do that
‫I'm going to go over to my main activity.
‫Okay? We are done with the user interface right now
‫and we are gonna write some code.
‫So first of all, it's very good idea to start
‫with writing the methods that we have defined
‫as on click methods in our xml
‫so that we don't forget them, okay?
‫I'm not writing it under on create
‫but rather under my main activity I'm outside of on create
‫I'm gonna say public void and I'm going to start with sum.
‫So this is how we create methods, remember
‫but we have to add, view view parameter over here
‫in order to specify this method is going to be called
‫by a view.
‫So if you get a error like this
‫just click over here and hit, hit alt enter
‫in your keyboard and as you can see it's imported over here
‫like import view and then say view.
‫So we are doing this in order to let us know
‫that this method will be get called by a view
‫like a button or image view.
‫So I'm going to continue doing that.
‫I believe we have deduct, view view, okay, and we are done.
‫Let's go for multiply.
‫So public void multiply, view, view.
‫And then for the last one we are gonna go
‫for divide public void divide.
‫And here you go.
‫Now we are done with all the public methods over here
‫in order to be sure
‫that we have the on click methods in our coding section.
‫So what do we want to do in this app?
‫We want to get the input of the user right?
‫We want to get the numbers that our user chooses to give us
‫and that we are gonna do some kind
‫of mathematical operation on those numbers
‫and we are going to display the result on a result text.
‫So we need our text we use in edit text
‫inside of our coding section.
‫We need to connect them first, right?
‫And maybe you remember how to do this thing.
‫So I'm gonna do this under on create
‫and we're gonna talk about why we are doing this
‫under on create.
‫So I'm going to start with edit text and here you go.
‫We have to do alt enter.
‫So edit text is the type that I'm defining over here
‫and you can call it whatever you want.
‫For example, I'm gonna call this number 1 text.
‫You can call it edit text, you can call it your name.
‫But after that you have to say find view by Id
‫and R.id. whatever Id you have given to your edit text.
‫Like in my case, it's already number 1 text.
‫So over here I'm choosing the ID that I have given in xml.
‫Now I'm gonna do the same thing for number 2 text as well.
‫I'm going to say find view by Id, R.ID dot number 2 text.
‫And finally we have our text view, right?
‫So I'm gonna call this result text and say
‫findview by Id R.id result text.
‫So beware that these are the same with this
‫but this is just a coincidence.
‫You can call this whatever you want, okay?
‫But this is a convention.
‫It is fine.
‫It is a good way to call this things with the
‫exact functionality that they're
‫they're going to do in the app.
‫So you can call this your name as well.
‫It won't, it won't break, it will work
‫but it's a good habit to choose good naming.
‫Okay? So as you can see,
‫I have all this texts over here like
‫edit text, edit text two and text view.
‫Now if I want to get text out of an edit text, I can do it
‫like this, like number1text.getText, okay?
‫And then if I say .toString like this
‫it will give us the string that user has typed
‫in that edit text.
‫We are going to discuss why are we adding some two string
‫or what is the get text and stuff later on.
‫However, I want you to understand
‫that we can use this get text .two string methods
‫in order to get the current string in this edit text.
‫But there is no point doing that under on create, right?
‫Because when we create this activity
‫the edit text will be completely empty like this.
‫So when a user taps one of these buttons
‫that's when we try and get the content
‫of this edit text so that it can actually take these numbers
‫and add them together and show the result, right?
‫So we want to do this thing like
‫number1Text.getText.toString in one of the methods
‫actually in all of the methods.
‫So if I write number1Text, can I reach the text,
‫the variable that I have defined under on create?
‫Of course I cannot because they are in
‫different scope right? We have talked about this.
‫So we have this coding block over here
‫and this coding block over here, we cannot
‫actually reach the variable that we have defined over here.
‫So maybe you can think something like that.
‫Yeah, let's just copy and paste things over here
‫and over here and over every method that we have
‫and it will actually work,
‫but it's not very efficient, right?
‫It's not efficient for us because we have to
‫copy and paste and it's not efficient
‫for memory consumption as well.
‫Every time a user taps on one of these buttons
‫then I will have to create this views from scratch.
‫So it's kind of inefficient.
‫So what, what do we do?
‫Remember we talked about is we can actually
‫define the variables and also the views inside of our class
‫but outside all, all the methods so that we can
‫reach them from anywhere we want,
‫inside of the class obviously.
‫For example, we have done this like, int myNumber
‫so we can reach my number from any method.
‫We can do the same thing for the edit text
‫or text view or image view or buttons as well.
‫For example, I'm gonna copy this
‫and paste it over here and put a semicolon the end.
‫Okay, so I'm going to do the same thing over here as well.
‫Number 2 text
‫and I'm going to do the same thing over here as well.
‫So this is defining, okay?
‫If I delete the types from here like this,
‫this is initializing.
‫So right now I'm initializing the views over here
‫assigning the values to them
‫and over here I'm defining them.
‫So right now if I write number 1 text or number 2 text
‫or result text from anywhere in coding section
‫then I can actually reach those.
‫So under on create or under some or under deduct.
‫And they will be initialized
‫they will be actually connected
‫to the related IDs under on create.
‫So it won't be any problem for me either.
‫So right now if I come over here and say number 1
‫as you can see, I can reach it.
‫So that's good.
‫Now we have defined all the views
‫that we are going to be needing
‫we are going to be using in this app.
‫Now we can actually move on to the coding section as well.