‫We learned how to associate objects with data binding variables.
‫Now we're going to handle the click events on views in this layout.
‫So I'm going to handle the click event on a button.
‫Here I'll create a button wrap content wrap content and the text would be click me.
‫I'll drag and drop it here in further constraints.
‫And this is good.
‫And by the way this is the text view.
‫And this is the button.
‫The ID of the button is button.
‫If you go back to the main activity here I'm going to handle the click event binding dot button dot
‫set on click listener.
‫And here I specify a toast message dot make text.
‫The first parameter is the context.
‫The second is the message you clicked the button, and the third parameter is the toast length.
‫Let me run and let me show you how to handle the click events in different ways.
‫And this is the first way.
‫If I click this button you can see the toast message appears.
‫This is good always.
‫When creating and using data binding, you can set binding dot and access the view that you want to
‫set a click listener.
‫To wit, there is another approach I can create a new function outside the Oncreate method here.
‫Function display toast message I will cut this to here, and instead of using this binding dot button,
‫set and click listener and call this method, I'm going to comment this and go back to the layout.
‫I'm using data binding to get rid of boilerplate code and make my Kotlin files very clean, and get
‫rid of UI related codes.
‫So the main purpose for me when using data binding is to get rid of UI related codes in KTX files and
‫Kotlin files.
‫Okay, so instead of using set on click listener and make these functions here I'm going to link this
‫button with the method here in XML and not in KTX files.
‫So instead of using set on click listener I'm going to use a function here called on click.
‫And I'm going to use an annotation as we used here at.
‫And please pay attention with me at Open Braces.
‫This is the annotation of data binding.
‫When you need to access something and you specify this lambda expression that it is referring for functions
‫in the main activity KTX file.
‫What is the name of the function display toast message.
‫Going back to this and here I'm going to paste it display toast message function.
‫And you can see there is an error here.
‫Cannot find identifier display toast message because I'm referring for the main activity.
‫And I don't create here a data variable referred for the main activity.
‫So to solve this problem either I can create a variable of type main activity or I can move this function
‫to the vehicle or another class.
‫The professional developers move all the functionality of buttons into a new standalone classes.
‫For example, here I will create a new Kotlin class and I'll name it as vehicle click handlers.
‫Click okay, and inside this class I'll move this function from the main activity into it.
‫And to create a toast message, you can see that classes don't have context.
‫I need to pass them in.
‫Uh, from the main activity or other activities to this class.
‫Inside the primary constructor of this click handler, I need to pass a var context type context alt
‫plus.
‫Enter to import the package of content and the context class.
‫And here instead of using this, I need to use this context pass as a parameter.
‫So what I have done here I created this class.
‫I passed context in its primary constructor in order to use it in the toast message, and I moved this
‫function from the main activity to this class.
‫Now let's go back to this activity Xaml file and I'll create a new variable here.
‫So professional developers separate between the data classes and the classes that contains functions.
‫And it's named as click handlers I'll create a new variable here inside the data.
‫Scoop and I'll name it as click handler, the type of this class.
‫Again, you need to specify the whole package and you need to specify the class it's named as vehicle
‫click handlers.
‫I'm referring for this class.
‫I need to access this method inside this class.
‫So to do that I'll scroll down to here and this button.
‫I need to remove this and pay attention to this convention.
‫I'll start with handler or click handler.
‫This is the variable that I created.
‫And here I can't access the method by putting dot display post message like this.
‫In order to access methods, you need to replace the dot with two colons and you can access the method
‫right away.
‫And by the way, we have an error here because we need to set a view parameter inside the display toast
‫message.
‫So this error will be solved by passing for this method a view parameter.
‫So data binding is not an easy thing you add to your applications because you need to pay attention
‫for every step and don't miss anything guys.
‫So here I created a click handler object vehicle click handlers.
‫And it's a variable.
‫I am assigning this variable to this.
‫And I'm going to access the display toast message.
‫By using this convention.
‫The two colons not one dot.
‫By using the two colons to access the click handler class and to link this vehicle, click handlers
‫with a variable here.
‫Inside the layout, I need to use what I used before.
‫I need to use binding dot click handler equals to.
‫I need to specify a new vehicle, click handlers and pass the context to this.
‫So here I am linking the click handler object with the click handler variable inside the layout, and
‫I am accessing this function inside the click handler class to be executed when the button is clicked
‫again.
‫Guys, don't miss these steps because many developers contact me having troubles and problems in data
‫binding.
‫Guys.
‫When dealing with methods, you should use two dots.
‫You should create a variable.
‫You should specify the view parameter inside the method, and you should link the click handler with
‫its object inside the main activity.
‫Run the application.
‫Here we go.
‫Click the button and you can see we have this toast message.
‫Congratulations guys!
‫We learned how to handle the click events on a button using the data binding library.