‫Instructor: Hi, within this lecture
‫we are going to continue working on our image changing app,
‫but this time, we're gonna write some code.
‫So that's why I'm going to go into this main activity
‫which is our coding section where we write our code.
‫Okay, as you can see
‫we have something called onCreate over here.
‫And this is a method.
‫This is what happens when this activity is created.
‫So for example, if I want something to happen
‫when user first sees this activity, first sees this screen
‫then I'm gonna write this under onCreate.
‫In fact, I don't have anything like that
‫in this app because I don't want something to happen
‫when user sees this activity.
‫Rather, I want something to happen when user clicks
‫on that button, right?
‫So onCreate is for creating something
‫or doing some operation before even a user sees this
‫before when this is exactly when this is created.
‫So this is created even before when user sees this.
‫So I don't need anything like that.
‫But what I need is actually to write a method
‫in order to do something when user clicks on this button.
‫So we are going to create another method for that
‫and this is called on click method, okay?
‫And this on click refers to clicking on this button.
‫So that's what we are going to do.
‫And also we have to define those views
‫into our coding section as well.
‫We have to connect that button
‫and we have to connect that image view to our coding section
‫so that a Java will know what we are talking about.
‫Like I'm gonna show you how to do this.
‫So first of all, let me create these methods.
‫I'm going to call this change image
‫and we start by typing public void, change image
‫and open close parentheses
‫and open close curly braces like this.
‫So of course you don't know anything about that.
‫So let me try to explain what is going on over here.
‫Of course, we are gonna deep dive into that
‫in the Java section afterwards, this section.
‫But I'm going to explain what's going on.
‫So change image is the name of our method, okay?
‫So this refers to our methods like a function.
‫So this is what is going to happen
‫when this function is called.
‫And we have the curly braces to actually define the scope
‫of this method.
‫So whatever we write inside of this curly braces
‫will happen when this function, when this method is called.
‫So if I write something over here on the onCreate
‫it will be done when onCreate is called.
‫If I write something over here,
‫it will be executed when change image is called.
‫And we are going to define it in a way that when a user taps
‫on this change button, it will be called, okay?
‫So we are going to connect our button
‫to this method later on.
‫And over here, as you can see in the image view,
‫we have an ID identification, which is image view.
‫So we are going to need that ID
‫in order to define that view into our coding section.
‫That's how you identify the different views like this.
‫So for example, we have something called button two
‫over here, and we don't need that.
‫We can change it to button,
‫or button 20, or button 10, or my button.
‫So all you have to say
‫just yes over here and it'll be changed like this.
‫And of course it's not a good convention to use button 20
‫but let's leave it at that.
‫For example,
‫I can change this to be image view six, for example.
‫And I have to say image view six over here.
‫So let's define that.
‫I'm going to write image view
‫with capital I and capital V as you can see.
‫So this is the class definition of image view.
‫This is the type that I'm writing, okay.
‫And in Java we write like this.
‫First we write the type of the viewer
‫of the object that I'm going to create.
‫And then we write the name of the object.
‫So actually we choose the name of the object, okay?
‫But we don't get to choose the type of the object.
‫So image view is something predefined in Java.
‫And over here, for example, I can call this image view like
‫with a small I, not uppercase, but lowercase, okay?
‫And then I can just make an equal sign
‫and open parenthesis and say image view, one more time.
‫I'm going to explain why I'm doing this.
‫And I'm going to say find view by ID.
‫And this will enable me to connect with the views
‫inside of my layout XML file.
‫So I'm going to say R.id.ImageView5.
‫So that was the ID that we have given remember
‫and we have to put semicolon at the end.
‫So what have we done?
‫We specified the type and the name
‫that we are going to create.
‫And we said find View by ID.
‫And actually we casted this view to be an image view, right.
‫Now, for example,
‫we can delete the second image view, okay?
‫Because before 2019 I believe,
‫a find view by ID alone was not sufficient
‫for underage studio to understand this.
‫It understood it was a view
‫but it didn't understand what kind of a view that was.
‫So we had to cast it to image view
‫or we had to cast it to a button, for example, like this.
‫So if you see anything like this
‫in tutorials online or in any code
‫that you're trying to understand, then don't be surprised.
‫For example, if you do the same thing for button
‫and as you can see, we are getting an error over here
‫which is an import error,
‫which means that button is not imported in our project.
‫And if you come across something like this,
‫all you have to do is just click on it
‫and hit alt and enter on your keyboard.
‫It will import the class
‫like this into your class, into your project like this.
‫As you can see, it's going to be imported
‫on the upper side of our project.
‫And when I do alt enter,
‫as you can see it imported this widget.
‫Now it's not giving me an error.
‫So I can call this button again with small B
‫and I can just say find view by ID or .id.button20.
‫So as you can see, I'm doing the same thing.
‫I'm creating a button.
‫So this is a button type.
‫So I'm naming this to be button
‫and now I can reach that button
‫by typing button with a lower case
‫and I can change all the attributes by here hitting a dot
‫or here like we did in the layout section
‫but we can now do it programmatically
‫with code from here as well.
‫Now, I have connected my views
‫with my coding section like this.
‫For example, if I say set text
‫it will just change the text on the bottom.
‫I don't wanna do that, but it's just the way it is.
‫And again, you don't have to use this, okay?
‫You can just delete this, but I'm just leaving this
‫as it is so that when you see something
‫like that later on with your own researcher or something
‫you don't get surprised, okay?
‫And these names don't even matter.
‫You can call this whatever you want, but the type matters.
‫So just follow along with me over here.
‫So we are gonna stop here
‫and continue within the next lecture.