Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.2 Event Handling With Lambdas

Lambdas and method references can save a lot of typing if you use them wisely. In this lesson, I'll show you how to make your code more concise by using lambdas and method references while handling events in your apps.

2.2 Event Handling With Lambdas

Welcome back. Let's move on to seeing how lambdas and method references can make Android app development easier. First of all, you need to know that a lot of Android interfaces are functional interfaces. The most well known of such interfaces are even-handling related interfaces. For the sake of an example, let us add a button to our layout and then add a click event handler to it. So open the layout XML file. You can delete this TextView and replace it with a button. I'll set its ID to my_button. Back in our class, we will have to create a reference to the button using the findViewByID method. To assign an onClick event handler to this button, we will have to use the OnClickListener interface. As you've might already know, this interface has just one method called onClick, which accepts a view as its only argument. So instead of creating an instance of an anonymous class that implements the interface, we can simply use a lambda, which takes a view as its only argument. Inside the body of this lambda, it's business as usual. I'm just gonna show a Toast here, but you are free to do anything you want. This syntax is obviously very concise compared to the syntax you would use in Java 7, but it can become even more concise. If your lambda is just one statement, you don't need these curly braces. And you can remove this semicolon at the end of your statement. At this point, our lambda is called an expression lambda. Again, this is just a minor difference in the syntax. There is absolutely no difference as far as the end result is concerned. Let's work on another example now, which will be slightly more complex. This time, let's try adding event listeners to two edit text widgets. So, in the layout XML file, add an edit text widget. I'm gonna position this at the bottom of the screen. And at the center horizontally. Next, set its ID to input 1. And remove its default text. Similarly, add another EditText widget. Add constraints to it so that it is positioned above the previous edit text. Set its ID to input 2 and remove its default text. Lastly, go to the text mode, and add an IME options attribute to both the widgets, so that the virtual keyboard displays a done button. So the value of the attribute should be actionDone. All right, back in the class, use findViewById to get references to both the EditText widgets you added. First, I'll get a reference to input1 and then a reference to input2. Okay, now let's say that we want to log a message whenever the user presses the Done key. So we are gonna need two event handlers, but both of them must perform the same action. This is obviously the perfect situation to use method references. So let's create a new method who's signature matches the signature of the method that's inside the listener. So this will return a boolean and accept three arguments, a TextView, an actionID, and a KeyEvent. Inside this method, we can simply check if the action value is equal to EditorInfo.IME_Action_Done and log some message. Let's just say, User finished typing. Now that we have our method, we just need to use its reference while creating the EditorActionListener. And we can reuse the reference while creating a listener for the other widget. If you run the app you will be able to see that both the listeners are working correctly. You now know how to use lambdas and method references in your Java 8 code. Thanks for watching. I'll be waiting for you in the next lesson where you will learn about Java 8 streams, which use lambdas extensively.

Back to the top