In this video, learn how Anko can simplify your interactions with Intents and the default Android Logger. By making use of Anko, you can make some of the Android APIs more friendly to work with.
- [Instructor] Let's see how Anko can simplify our interactions with intense and the default Android logger. It's a library so we just need to make sure we have the dependency in our build.gradle file. At the time of this recording, it's 0.10.3. And then here on line 34 you can see that it's part of the Anko commons component. Now let's start by updating our planet list activity. We want this class to use some of the Anko extension methods.
So let's clean up the on click listener that's inside of our planets adapter which starts here on line 39. Right now we create an intent that contains the planet id as an extra. And you can see that here on line 42, we then provide this intent to the start activity method of the context class here on line 45. But there's a lot of boiler plate code involved. So let's see how we can achieve this goal with less code using Anko extensions.
So the first thing we're going to do is comment out this code here. And then we're going to start by adding Anko extensions. So let's give ourselves a little bit of space. And the first one we're going to use is the start activity function. The start activity function of Anko is generic. So inside of the Anko brackets we need to provide the type. In this case it's going to be the planet detail activity.
Let's go ahead and include the import. Now before we get started with adding in our function details, let's look at the definition. Here on line 30, you can see that the start activity function is an extension function on the context class. And you can provide various pairs that represent your extras. And then finally, for the implementation, it just calls Anko's internal start activity method. So let's switch back to the planet list activity and provide our extras.
So when we come here, we see that our extras, based on what's here on line 44, is the arg item id for the key and then the item id of the planet. So let's just copy this to simplify things, and then we can paste that here onto line 48. And instead of the comma, since we're using pairs, we're going to use the two function. And now we have supplied our parameters. Now that's simple enough but look how we took all of this code that was here before and we reduced it to just one line of code.
Now there's one final feature of Anko that I want to show you before we run our application is, and that's the Anko logger. This let's you skip creating tag variables in each of your classes as well as being forced to use the static log class. The easiest way to use it is to reference its interface in your class definition. So let's do that here on line 21. We'll add a comma, and then we'll include Anko logger. Now if we go back to our on click listener let's add a warning log that contains the name of our planet.
So right before we start our activity, we're going to make a call to the Anko logger warn method, and then we're going to supply clicked on a planet. And that's it! So let's run the application again to see how everything plays together. We can switch over to our emulator. Now first, switch over to our emulator. No first, let's click on one of the planets.
And yes, we were successfully taken to the detail screen, so that worked perfectly. Now if we come back to android studio, we can take a look at the logs, and we see we have a warning log which states, clicked on a planet Jupitor. So by making use of Anko, we've been able to make some of the android API' friendlier to work with
- 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