Anko provides several different artifacts that you can use to enhance your Android development. In this video, look at the use of the Anko Snackbar and Fragments extensions.
- [Instructor] Anko provides several different artifacts that you can use to enhance your android development. In this video, we'll look at the use of the Snackbar, and fragments. So first we need to make sure we have the following dependencies in our build.gradle file. The anko-design dependency here on line 35, and the anko-support-v4-commons dependency on line 36. Since these aren't core to Anko, they're found in the different artifacts. Now we're going to be updating the PlanetDetailActivity class in order to take advantage of these new types of Anko extensions.
Previously, we created our own extension on the view class to handle some limited interaction with the Snackbar. And that's here on line 21, where we created our own showSnackbar extension. But with the Anko Design Library, we no longer need to do that. So we can replace this code right here, with very simply, a call to Snackbar. And let's make sure we include the Anko Design import. That's just snackbar.
But you'll notice that we have an error, because this particular extension requires having a view as the first parameter. So we can simply supply the view that we receive in our lambda. And now everything is good to go. This will mimic exactly what we had before. However, we're no longer locked into having the hard-coded action. Because there's another variation of this extension function that takes a lambda as it's last parameter. So now, let's go ahead and update our Snackbar with the action that we'd like to take.
So right after we have our message, we can also provide a string for our action. So let's do that now. And to keep it simple, we'll call it Action. And finally, we can provide a lambda which will contain the action we want to take. And in this case, let's send a toast message. So we'll use the toast extension from Anko, and we'll just say "inside the action". Now let's run our application so we can try out our changed Snackbar.
We can switch over to the emulator now, and let's just click on a planet from the list. And we can click on the pencil icon, and notice when we click the action, we see our toast message. Great, now let's also see how the Support V4 variation of Anko, can help us when we're working with fragments. So back over to our PlanetDetailActivity, and we're still here in the onCreate method. But let's focus on how we pass our arguments starting with line 31.
So instead of creating the Bundle directly, we can take advantage of the withArguments extension. So this is how we're going to do that. Right after we create our fragment on line 34, let's hit an Enter, and then we're going to call the withArguments extension on our fragment. This takes in a Pair. And for our argument, we're going to use the key ARG_ITEM_ID, so we can copy that and past it in. The to operator, and then everything that's contained here from our intent.
Now this is pretty cool because not only do we create the fragment, we can also provide the arguments in one foul swoop. So let's go ahead and delete lines 32, and 33. And then we also no longer need to set the arguments explicitly. So we now have less code, and it's more readable. So far we have seen how Anko can assist us in working with Android API's in a more concise, readable manner. Although some of the changes tend to be a little subtle, as you use them more and more throughout your codebase, you'll see how much nicer they are 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