 Hi, this is Christian. In this video, I'm going to show you how to register event listeners to two buttons and using two different methods. Okay, so here is my layout for the activity main. I'm using input field for, this is a edit text field. Enter data, when I click submit, it's going to grab the data from here and going to append the data to the output field out here. This is a text field as well. And then we'll also click the clear all button. We're going to clear both data from here and down here. So, first let's go to the main activity. And here's the main activity. I'm going to create a function down here. So, we'll call it public void and I'll call it unsubmit. And I'll take the view and then here, let me import the view first. Just press alt enter and then import that for you up here. And so, I'm going to go up to global space and declare two, maybe three variables, maybe two. One is for the text view. This is the text input and I do another one for the text output. Okay, so just those two. And then in here, well, yeah, nothing much here. So, that's all I need for this one here. I'm going to do another one here as well to also something similar, public void on clear. We clear all. Okay, again, view, view. We may not need the view, but we'll just put that anyway just in case. The view here is actually the object that you pass to. The view here will represent that view. So, if you attach this function to the button, the view will represent the button. If you attach this to a text view, it'll be the text view. So, whatever object it attaches to, the view is representing that object. Okay. And so, that is all I'm doing. Now, when I submit the data, submit, I'm going to grab the text from the input text field. I'm going to get the text, the string, and then, you know, just basically append that to the output here. Okay. So, here, I'm going to go and say txt. Well, before I do this, I want to get the reference to these objects. So, right in here, when I click the start, I'll do it once because this function runs only one time. Okay. It runs on one time. And every time we go back to this activity, it runs every time. But when you run this, initially, it just runs it once. And so, you want to set those right in here. And I'm going to say txt input equal to, we're going to convert that to text view. And we used to find up id, r.id dot txt input. Okay. Edt, I call it Edt input. Actually, it's an error. Sorry. This would be an edit view. Not edit text, not a txt. I call this Edt. Just make it a little bit different. And so, this would be Edt. Okay. And then here would be edit view text. Okay. So, I find the reference, I do the same thing for the other one. This would be the txt output equals, this would be the text view. Then find by dr.id dot txt output. Okay. So, I got those references. And then I'm going to go here and when I click the button, I'm going to, you know, get the text from the edit view, move that to the text view. So, I would do something like string, just call it txt text from the edit input. No, not that one. Import that. Get text. And you want to convert it to string. Okay. So, to string and then here. And then finally, I'm going to call the txt output dot set text. Call that set to function. You pass in the, well, actually, I'm not going to set text. I'm going to use the append. Okay. Set text means it will overwrite everything you have in the text. So, if that's the, if that's what you want, then you call that. Otherwise, you call the append function. You pass here the txt. So, it will keep adding that to that text. Okay. And then at the same time when I enter the submit button, I also want to clear the text in that field. Okay. So, I want to go and say edit t input dot set text. You set it to an empty string. Okay. You just basically clear, clear that input field. And then on the clear all is basically I'm going to clear both of them. So, you can just do very simple the edit input text dot set text to be empty. And then you do the same thing for the other one. txt dot set text to be empty. So, you clear both fields. Basically, that's it. Okay. So, let's see what this looks like. Now, before I run this story, we want to go to the main activity over here again. And we're going to attach a event, a function to the submit button. So, you click the submit button here. And the right side, if you don't see these attributes, make sure you turn on your attributes here. And you can look for the unclick. You can, you know, scroll down and find it. We can look at the search icon here and search for unclick. It will show up really quickly here. Okay. So, here's the unclick. And since we already created the function and the code, they are available right here for us to choose from. So, if you click the drop down, you see it's right here. So, I'm going to attach this on submit function to the unclick event. So, this is an event listener. Actually, you attach to that directly to the unclick event. So, that is one way how to do this program. Okay. So, now, we can save this and run and see if it works. Okay. So, I'm going to go and run and it's compiling. So, here is my little app here. I'm not sure what's going on. Let me just do it again just in case one more time. Okay. So, no errors. All right. So, I'm going to go here, enter some data. You know, one, two, three, four, enter submit. And as you can see, enter, we move the data down here. This field clears automatically. I put here, again, maybe five, six, seven, eight, submit. And you see that it pins the data down here. It attached to the text of string down here because I did not, you know, include a carriage return. So, you probably want to do that as well. So, that looks good. Okay. I'm going to add a carriage return. And then I'm going to go and then, you know, attach another event to the clear all button using a different method. Okay. So, back in here. So, I showed you already how to do that using the unclick event. Attach it to a function this way. Okay. Now, let's go back to the source code. And when we attach the append here, I'm going to add a at the end of this string here. At the end, I'm going to add concatenate that with the slash n. This is a escape key for a new line. Okay. So, after I append the text, you know, generate a new line and then it's ready for the next line. Okay. So, that's good. So, now, the second one is we're going to create what's called an event listener. Okay. It listens to an event and you do that programmatically in your code here. All right. So, to do that, we have to, again, register the or find a reference to the button, which is the clear all button. So, down here, I'm going to create a button object, the button. We call it btn clear all. And this is going to be assigned to a button defined by dr.id.btn clear. Okay. That's the clear button. So, now, that reference has been created. I'm going to go in and import the button class. Okay. So, notice you have to add every time you create a text view, button view, and a lot of those views. If you go up here, you can see that in the import statements, you see that these right here are in the same class called widget. Okay. So, instead of doing this every time, you can, you know, put all these off and just put a star right here. Okay. So, that, you know, all the classes that are part of the widget or subclasses of the widget, you don't have to do that in port every time. Okay. Just a shortcut. So, now, I created a reference to the clear all button. I would just call clear. It's okay. So, now, we're going to register to this button using a function called setonclickListener. There are a couple of them. Okay. The one that is most popular is the onclickEventListener. So, you can call this function and click on it. And then to this function, we're going to pass in event handler, which in this case is called the view. So, we have a new view dot. You see here is onclickListener. Okay. This one here is named the same or similar way to the set on the function over here. So, if it's a set on in a hover, you have view dot on hover or something. Okay. So, you click on this function here and then you see that this is a, you're going to treat this like a class like that because, you know, this new here means it's a class even though this is an interface, but it allows you to create a, what's called the anonymous inner class. Okay. This is the anonymous inner class. Okay. So, you see this red line here because I didn't do it the other way. Usually, if you type it out and you hit the tab key, it will insert everything for you. But here if your mouse over this is that you must implement any methods in there. So, if you click this implement this here, you'll see just you see this one function called onclick, you must implement because it's an abstract, the view is an abstract class. Okay. So, every abstract class, every extra functions or methods you must implement. Yeah, I think this is the interface. Okay. So, when we click that button called clear, it's going to look for a click event because we set on a click event. So, it listens to a click event. When it clicks, when we click the button, it's going to load this function, goes in here and then trigger this handler. This is called a handler. It's a function or event handler. It handles what you need to do here. Okay. So, instead of, you know, doing my coding here, I already have a function called unclear all. So, I would just call the function right in here on clear all. And then I pass in the V, right? This V here is this view here, right? You pass that to that and then it satisfies the requirements down here. So, that's all I'm doing here. So, save this now. So, go back and run your code. And now hopefully, let me close this. Okay. Let me just, you know, do something here because I think something's wrong here. One of my previous old apps keep running. Okay. So, now if I go and type in something again, 123, hit submit. Okay. 456, submit. You see that now it, you know, adds on a new line. And if I click clear all, it wipes the data here. Up here it's already empty when we submit it. So, for example, if I put 123, submit. If I type in 456, and if I don't hit submit yet, if I click clear all, you see that both texts are been deleted. Okay. So, that's how you attach or register events to an object. In this case, it would be to the buttons. I showed you two ways. You can use the event listener using, you know, IDs or you can attach directly to the object down here over here using the GUI interface. So, either way is usually fine. But you'll find that later on, it's actually much easier to do it in the code. So, again, if you have any questions, let me know. Thank you.