From the course: Intermediate Kotlin for Android Developers

Working with extensions

From the course: Intermediate Kotlin for Android Developers

Start my 1-month free trial

Working with extensions

- [Narrator] Extension functions and properties let you extend the class without inheriting from it, or even owning the code. For example, in Android, we work with many Java-based APIs, which aren't as concise as we'd like. Let's look at this simple example. We often need to change the visibility of views on the screen, so it's typical to see code like this spread throughout your activity or fragment classes. But wouldn't it be nice to have something more concise with improved readability like this: Kotlin allows us to do that by means of extension functions. Here's the extension function that we can create to achieve the call to visible that we just saw. Now let's break this down. First we start with the "fun" keyword, since we want this to be a method that we can call. Next, we have what's known as the receiver type. This is the class that we want to add our method to. Here, our receiver type is the Android view class. Then we have the method name and any associated parameters that we want to pass to our extension function. In this case, we don't need any parameters, so the parenthesis are empty. Finally, we have the keyword "this." This is referred to as the receiver object. It gives you access to properties and members of the class while respecting encapsulation. So you won't have access to private or protected members of the class. And like most things in Kotlin, you can simplify this by dropping the "this" keyword, as Kotlin already knows what you're referring to. At this point, it probably seems a little bit like magic. How is it that Kotlin allows you to just tack on methods to other people's code? Well behind the scenes, it's not that mysterious. If we look at the decompiled code, we'll see that it's by means of static methods that Kotlin allows us to do this. Don't obsess over the syntax here. The key takeaway is that nothing magical is going on. Just clever use of what's already available on the JVM. Now sometimes you may want to extend a class and use the property syntax over the function syntax. Something like this. See how recalling is visible on the "fab" variable? Well here's how to achieve it. Instead of the "fun" keyword, we're using "val" because we now want to treat it like a property. You still have the receiver type, just like before, but now there's also the addition of the "get" method. This is really important, because extension properties don't have any state, so you need to provide the implementation of the "getter" method. In this example, we want to return true if the view's visibility field equals view dot visible and false otherwise. It's just a shorter syntax that we can use in our code. Extension functions and properties are a great way to eliminate the many utility classes that we would invariably have when working with Java in Android. So we can easily go from something like this to this with minor code changes. Why not give it a try and implement the extension function for the "gone" method based on what you now know.

Contents