From the course: Android Development Tips

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Handle events with Kotlin lambdas

Handle events with Kotlin lambdas - Android Tutorial

From the course: Android Development Tips

Handle events with Kotlin lambdas

- [Instructor] Kotlin's overriding purpose is to reduce the amount of code you have to write compared to Java. One of the most common bits of Java boiler plate code in Android is the use of anonymous interface implementations as event handler objects. You can implement that sort of code in Kotlin, but it doesn't look very good. In this version of my application, my event handler is implemented in my layout file. It's called ContentMain.xml, and there are two buttons declared in this file, one called run button and the other clear button. They both have on click event handlers declared in the layout as xml attributes. I'm going to get rid of the on click attribute for the run button, and then I'll create some Kotlin code that will take care of that in the main activity file. As always, when you want to declare an event handler, you need a reference to the button or other view object. I'll declare an immutable variable that I'll name run button and I'll get its value with find view by…

Contents