From the course: Android Development Tips
Handle events with Java 8 syntax - Android Tutorial
From the course: Android Development Tips
Handle events with Java 8 syntax
- [Narrator] Starting with Android Studio 2.3, you can program with some Java eight style syntax. Including lambda expressions and method references. If you're using Android Studio 2.3, this is enabled by an experimental compiler chain called Jack and Jill. Starting in Android Studio 2.4 though, the Jack compiler is deprecated and now Java eight syntax is supported in the core building process. I'm working in Android Studio 2.3 in this example so I'll show you that version using the Jack compiler, but then I'll also show you what you need to remove if you're using Android Studio 2.4 or later. In this example, in my on create method, I have references to a couple of button objects that are declared in my layout. I'm using traditional find view by ID code and I'm getting the references here to M Run button and M Clear button. Then, I'm creating implementations of the on click listener interface and then adding the code I want to execute to the on click method. And this all works fine. When I click the buttons in my application, I get the expected behavior. If I click Run Code, I'm adding information to the console, and if I click the Clear button it clears the screen. If you use Java eight syntax though, you can significantly decrease the amount of boiler plate code that's needed. The first step is to configure your project to use Java eight. I'll go to my application module's gradle file. And then because I'm working in Android Studio 2.3, I need to enable the Jack compiler. This goes inside the default config element. Type Jack Options and then in a code block add Enabled and then True. Don't add a semi colon, this isn't Java. This is just a configuration declaration. Now, also inside the Android element but outside any of its child elements, add a compile option section. Inside compile options, add source compatibility. As you type you might get a pop up list, and if so, you should be able to select Source Compatibility from there. And then set that as Java version dot version one eight. Now duplicate that line of code and change source compatibility to target compatibility. So the source compatibility affects the compiler and the target compatibility affects the device on which the application is running. Re-sync your gradle file. Now if you're working on Android Studio 2.4 or later, do exactly the same thing for the compile options directive, but just don't put in the Jack options directive. You'll still be enabling Java eight. After you sync gradle, go back to your main activity class. And you should now see that the on click listener implementation is disabled. That's a visual indication that you can simplify this code. Place the cursor into the first on click listener call. And use an intention action. Hold down Alt or Option and press Enter or Return. And choose Replace with Lambda. And now all those lines of code are reduced down to a single statement. This argument V, represents the view object that initiated the click event. The call to run code receives that view object and if I control or command click on this method call, I'll jump to the reference. Which is designed as an event handler method, receiving a view object as its only argument. I'll do the same thing for the clear button listener. Clicking in, pressing Alt Enter or Option and Return and once again replacing with a lambda. And now I'll run my application on my device. And the code still works exactly like it did before. But with a lot less code in my activity class. Now notice that these calls, these lambda expressions, can be reduced even further with method references. Click into one of them, and once again use an intention action, and this time replace the lambda with a method reference. The method reference says I'm calling the run code method that's a member of the current object. This, the double colon is the method reference operator. I don't need to be specific about how the view object is being passed in as an argument. This follows what's known as a functional interface. An interface that has a single method or function. The set on click listener method is expecting a particular interface which only defines one abstract method, and the run code method follows that method's signature. Now I'll do the same thing for the clear button. Converting the lambda to the method reference. And once again I'll run the application. And once again the code still works exactly like it did before. So that's lambda expressions and method references. Two Java eight syntax features that are now available for Android developers. Again, if you're in Android Studio 2.3, use the Jack compiler. If you're in 2.4 or later, just eliminate the Jack options declaration and you'll be programming with Java eight.
Contents
-
-
-
(Locked)
Communicate with Snackbar messages6m 15s
-
(Locked)
Manage view components with Butter Knife6m 41s
-
Create a reusable dialog class9m 22s
-
(Locked)
Convert a layout to ConstraintLayout7m 13s
-
(Locked)
Manage global data with singletons5m 37s
-
(Locked)
Convert image files to WEBP format6m 5s
-
(Locked)
Create an Espresso user interface test5m 18s
-
(Locked)
Create classes for Android with Kotlin5m 12s
-
(Locked)
Run background tasks in intent services8m 51s
-
(Locked)
Send broadcast messages with EventBus8m 44s
-
Handle events with Java 8 syntax5m 51s
-
(Locked)
Improve forms with design layouts7m 55s
-
(Locked)
Display draggable rows in a list of data8m 1s
-
(Locked)
Manage persistent data with Realm10m 23s
-
(Locked)
Play an audio file from assets8m 56s
-
(Locked)
Send an SMS message from an app7m 56s
-
(Locked)
Get a list of audio files on a device8m 32s
-
(Locked)
Play sounds with SoundPool8m 45s
-
(Locked)
Display the build date of an APK file6m 47s
-
(Locked)
Build Kotlin classes with properties7m 32s
-
(Locked)
Manage versioning with Gradle variables6m 8s
-
(Locked)
Use reporting tools from the Google Play Console4m 44s
-
Make web service calls with Retrofit10m 39s
-
(Locked)
Make asynchronous calls with Retrofit4m 6s
-
(Locked)
Send request parameters with Retrofit4m
-
(Locked)
Upgrade a project to Android Studio 3.04m 47s
-
(Locked)
Clean up calls to findViewById()2m 58s
-
(Locked)
Create adaptive launcher icons5m 47s
-
(Locked)
Use XML and downloadable fonts6m 40s
-
(Locked)
Explore a device's file system4m 55s
-
Profile an app's memory usage3m 52s
-
(Locked)
Manage an SQLite database with Room9m 17s
-
(Locked)
Manage background threads with Executors5m 32s
-
(Locked)
Track vital stats in a published app3m 4s
-
(Locked)
Inspect database files with DB Browser4m 41s
-
(Locked)
Execute tasks with JobScheduler7m 29s
-
(Locked)
Create Room entity classes in Kotlin8m 22s
-
(Locked)
Create Room DAO classes in Kotlin4m 13s
-
(Locked)
Upgrade to Android Studio 3.13m 22s
-
(Locked)
Convert a project to use Kotlin3m 20s
-
(Locked)
Handle events with Kotlin lambdas6m 13s
-
Manage menus with when3m 44s
-
(Locked)
Define global constants with Kotlin6m 12s
-
(Locked)
Use Kotlin data and enum classes5m 24s
-
(Locked)
Create inheritance hierarchies in Kotlin6m 8s
-
(Locked)
Manage data collections in Kotlin5m 45s
-
(Locked)
Sort data collections with Kotlin5m 33s
-
Filter data collections with Kotlin3m 52s
-
(Locked)
Create Kotlin extension functions2m 31s
-
Use support functions from Android KTX4m 30s
-
(Locked)
Use virtual devices with Google Play3m 50s
-
(Locked)