Skill Level Intermediate
- [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 id and I'll declare this as a view object.
In Kotlin, this is how you indicate the type that's being returned by the function. Then I'll pass in the id, r.id.run_button. Next, I'll create the click listener. I'll call set on click listener, and here, I need an instance of the view.onClickListener interface. In Kotlin, you declare an anonymous object with the object keyword and then you declare its type. View.onClickListener and from there, I'll add a code block, and then I'll start typing on click and I can auto-complete that function and there's my code.
Now when this code is called, I want to call an existing function called run code down here, and I'll pass in V. Notice that there's an error condition, and that's because the type that I'm receiving in the on click function is a nullable object. That's what the question mark means. Then I'm passing that value to run code, but it doesn't know that this value might be null. To fix that, I'll just add the question mark there and now the two types are the same. So now I'll click the run code button, and everything's working as expected.
Within this function, I'm receiving a reference to the view object that generated the event, and I can use that to find out which button was clicked. So for example, I might call my log function and I'll pass in you clicked the button with id, and then I'll append the value using resources.getEntryName, and I'll pass in the view object's id with view.id. Now to fix this little error, I need to add an assertion that says this value isn't null and I'll do that with a double exclamation mark expression.
And now when I run the application and click the button, I'm told I clicked the button with the id run_button. So far so good. This code is working. But honestly, this Kotlin code is just as verbose as the Java version. I'm created an anonymous object, it's an implementation of the on click listener interface which has a single function or method called on click. I can simplify this enormously by replacing all that code with a lambda expression.
And Android Studio will do the magic for me. I'll place my cursor after the opening parenthesis, then I'll use an intention action and select convert to lambda. And the code is now a lot shorter. This is the lambda. It's declaring a variable named v and then passing that variable in as a parameter to the run code function. This works because the interface has a single function or a single method. It's called a functional interface, and that method is receiving a single parameter.
So the compiler knows where to put everything. And if you run the code exactly like this, the functionality won't change at all, but I'm doing it with a lot less code. Notice that there aren't any parentheses around this code block. I'm calling a function called set on click listener, and in classic programming style, you would normally need a set of parentheses around a parameter. I'm passing a function as an object. This works because in Kotlin, those parentheses are optional.
You can put them back in and everything will still work as it did before, but really, it's just extra code, so I'll delete them again. And you can make this even more concise in another way. Instead of naming the parameter that you're passing in, you can use a special keyword called it. And once again Android Studio can replace this code for you. I'll place the cursor after the declaration of the variable and then use an intention action and say replace explicit parameter v with it.
And now I'm passing in a value to the one and only parameter in the one and only function in the interface that's expected by set on click listener. All of this information is available to the Kotlin compiler and in Kotlin, if it doesn't need the syntax to figure it out, that syntax will frequently be optional. I'll test this bit of code again, and it still works. And then finally, I'll reduce the code even further. Find view by id is returning a reference to the button, and then I'm immediately calling set on click listener.
I can get rid of the reference here and then close all that code up into a single line. And now I've declared an event handler with a single line of code. So because of all these syntactical optimizations, Kotlin code can be a lot briefer than the Java equivalent. Lambda syntax and the associated keyword it can significantly reduce the amount of code you have to write to accomplish simple Android tasks such as listening for and handling events.
Q: Why can't I earn a Certificate of Completion for this course?
A: We publish a new tutorial or tutorials for this course on a regular basis. We are unable to offer a Certificate of Completion because it is an ever-evolving course that is not designed to be completed. Check back often for new movies.