Kotlin Android Extensions is a plugin that lets you access views from your layout XML as if they were a regular property inside of your Android View classes. In this video, learn how to set up your project to take advantage of the plugin.
- [Teacher] Kotlin Android Extensions is a plugin that lets you access your views from your layout XML files in a very simple way. Let's see what we mean by that. Here's a snippet of our layout file. We're showing this one in a TextView with an id of planetDescription. Now if we wanted to set the text of this TextView what do we typically need to do? Let's see. First we create a property for it in our class. This is where we specify the name we want to use and the type.
Next, let's say this is a fragment. You would get the view from the findViewById method and then set the result to our property above. And then, finally, potentially somewhere else we get access to our property and then we set our desired text. This should sound very familiar. But if we add the Kotlin Extensions plugin we get to bypass most of these steps and we end up with just this. We can use the id of the element from our layout XML, access it and then modify it.
We don't need to do any preliminary setup. Let's see how this works by moving over to Android Studio. As I mentioned, it's a plugin, so we need to apply it in our application's build.gradle file. It's right underneath here on line three. We're going to apply the plugin. And it's kotlin-android-extensions. Let's add another line and then we can go ahead and sync.
And now that that's completed we can start to work with the plugin in our classes. So let's start with the PlanetListActivity class. Now the first thing we're going to do is delete the use of the findViewById call. This is the whole purpose of using the Kotlin Android Extensions plugin. So we're going to delete line 22. And then we're going to receive an error saying, "Hey, do you want to import this?" So let's look at our different options. We can either just import the id or we can use an import from kotlinx.android.synthetic and then a reference to our planet list activity.
And that's the one we want to use. Let's do the same thing for our recyclerView here on line 25. Notice how it's called planet_list in our XML file? So let's just use the same name. We'll copy that name and then we'll delete this line of code because we no longer need the findViewById and we'll replace recyclerView with planet_list. And once again, we're offered to import it which is exactly what we want to do.
Now let's scroll up to the top and take a look at the import for these particular statements. Notice that they start with kotlinx.android.synthetic. Then we have the source set for the view elements that you want to access. So this file is in the main source set. That's why it's included as part of the import. Next is the name of the layout file. And then finally, it uses a star in order to make it easier for you to access all of the synthetic properties in one go.
And that's it. That's really all you need to do and you're up and running with Kotlin Android Extensions.
- 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