Anko provides you with the ability to connect various listeners to your layout code. In this video, learn how to attach actions to the onClick and onLongClick listeners. Additionally, learn how to interact with your AnkoComponent's owner property.
- [Instructor] Anko provides you with the ability to connect various listeners to your layout code. Let's see that in action. The first thing we need is the dependency for the Anko coroutine's SDK. That's here on line 38. Just like with the layout support above it, the number that you provide behind the SDK artifact is based on our min SDK version for the application, so that's why we're using SDK 21 dash coroutines. Now, let's move over to our Anko component so that we can add our first listener.
What we want is when someone clicks on a planet's description text, then we're going to show them a toast message. So, the first thing we want to do is come to line 20 and add a little bit of space for ourselves. And then we're going to call into the onClick function. Next, we're going to use toast and we're just going to put a simple message. Hey world from Anko. Now, how is it that we can include this on click listener here inside of our Anko DSL? Well, just like with most things in cotlin, it's by means of an extension.
So, if we click on onClick here, we can see its definition. It's an extension on android's view class, it takes in a context as well as a handler, and then it calls this set on click listener and launches a coroutine. Now, we're going to talk more about coroutine shortly, but then main takeaway here is that Anko uses simple coroutines to handle the listener interactions. So, let's close this down. And there's one final thing I want to point out here. OnClick gives us a warning that coroutines are experimental, but experimental doesn't mean unstable, so they're definitely ready to be used in production.
But to get us past this warning, let's use a quick fix and enable them. So, we're just going to alt enter, and choose enable coroutine support. And this is going to update our build dot gradle file with this little bit of code that you see down here starting on line 54. So we can go ahead and sink our project and now we're going to have coroutine support available to us. Okay, great. So, let's go ahead and run the application again and this time we should see that toast message whenever we click on a planet's description.
So, we can switch over to our emulator. Let's just choose a planet, click on its description, and then we see hey world from Anko. Great. But let's see what else we can do with our listeners. This time we're going to add a long click listener to the planet's composition text view. So, we're just going to move back over to Android Studio, open up that Anko component, and then come right here underneath line 32 and we're going to add our own long click listener.
But this time instead of just showing a toast, we're going to call a method on our owner class. So, remember, we marked the planet detail fragment as being our owner, so we're going to add the method there. Let's switch over to the planet detail fragment and we're going to add a very simple method that takes us to space.com. So, we're going to call this one go to space website. And then we'll take advantage of another Anko shortcut, which is browse.
And then we can just specify the URL that we want to go to. And that's going to be W-W-W dot space dot com. Great. Let's switch back over to our Anko component and inside of our long click listener, all we have to do is call the owner property and say go to space. Just like that. So, let's run the application again. Switch back over to our emulator.
Let's click on a planet, I guess choose Venus this time, and then we're going to long click on the planet's composition. It's going to ask us if we want to open this web page with chrome, so let's just say always. And we're taken to space.com. There are even more extension methods available on the view class and I encourage you to play around with them and see what you can create.
- 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