In this video, take a look a few more extension functions that are really helpful in your Android apps. Learn how to reuse common code across your files as well as how to create an extension function with a lambda parameter.
- [Instructor] Let's take a look at a few more extension functions that you'll find really helpful i0n your Android apps. For this, we're going to update the Planet Detail Activity class. And we'll start here with the onCreate method. Notice how we have an OnClickListener for our fab, which starts on line 21. Now let's say we want to show snackbars in other parts of our application with this same exact logic. Well we can reuse this code by means of an extension function. So let's copy everything we have here on lines 22 and 23, so we can use it as a reference later.
Now we're going to create a separate file, which we call common extensions, to hold everything for now. And this is going to be a Kotlin file, and as I mentioned we'll call it: CommonExtensions. So let's go a head and add our extension to Android's view class. So first we'll start by using the fun keyword. It's going to be on the view class and let's call it showSnackbar. For now, let's not worry about the parameters.
We're just going to paste the code that we had before from our fabs OnClickListener. The next thing we want to do is fix the error that we're having with the unresolved reference to view. Since the view is our receiver, we can change it to this. Now all the errors have cleared away. But in order to make this method more flexible, let's allow the person to specify their own message for our snackbar. So we're going to create a variable here on line six that we call message.
And it's going to be of type string. So then instead of having this "Replace with your own detail action" we'll just pass in our message variable. Just like that. So now if we switch back to the Planet Detail Activity class, we can replace this code on line 22 and 23 with the call to our extension function. So we'll just use view.showSnackbar and let's set our message to something simple, "My message".
Now if we run our application, notice that our behavior is the same, except the message has changed. So we can switch over to our emulator and now when we click on one of the planets, and click the fab, our snackbar now says "My message." So just what we wanted. Now let's say you want to find even more useful extension functions for your Android apps. My favorite website for this is kotlinextensions.com. On this website, you can find a curated list of some of the most commonly used Kotlin Android extensions.
Some of my favorite ones are on the View class. You can see ones for getting strings, showing the keyboard, as well as, the show if and hide if functions. Why don't you try out some of these extensions and see how they can help make your code more readable.
- What Kotlin has to offer
- Working with lambdas
- Common Android extension functions
- Kotlin Android Extensions overview
- Making use of the Anko library
- Working with coroutines
- Nullability and collections