From the course: Intermediate Kotlin for Android Developers

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Common Android extension functions

Common Android extension functions

From the course: Intermediate Kotlin for Android Developers

Start my 1-month free trial

Common Android extension functions

- [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…

Contents