Anko lets you simplify your interactions with the Android SDK, by means of extension functions and properties. In this video, get an overview of some of its main artifacts: commons, layouts, and Coroutines.
- [Narrator] Anko lets you simplify your interactions with the Android SDK by means of extension functions and properties. It's a library that has several main artifacts: Commons, Layouts, SQLite, and Coroutines. We'll be covering everything except SQLite in this course. Let's take a quick look at what each of the other three components contain. Starting with Commons, this artifact provides you with lots of helper functions and properties to deal with intents, dialogs and toasts, logging, and dimensions and resources.
For example, if you wanted to create an intent to launch a new activity with some extras, this is how you would do it in Java. Now if you use the Anko Commons library, this is how you can create an intent that does the same thing, just much more concise. First, there's an extension function available on the context class, 'intentFor.' It takes in the class type that you want to launch. And then, you provide a variable list of pair objects.
Each pair takes the string key for your extra, and then using the two infix function, you can provide the extra value. I personally enjoy working with this syntax inside of my applications. Another feature of the Commons artifact is DSL-like syntax for working with dialogs. Here's an example of creating an AlertDialog, setting the message, and adding actions to the 'ok' and 'cancel' buttons. That would generate the following AlertDialog with your message and button-handlers properly wired up.
And there are additional helper functions available in Commons. It's worth it to spend a few minutes exploring them. Now let's move over to Layouts with Anko. The Layouts artifact allows you to create Android Views without using XML, and it does this by means of a DSL syntax, and helps to promote component reuse. We're going to spend time walking through all of the details shortly. But for now, I wanted to show you an example of what the Anko Layouts DSL looks like.
This code would create a linear layout with an input field and a button. Finally, the last Anko component we'll explore is Coroutines. Coroutines are Kotlin's answer to the asynchronous programming problem. The Anko artifact chips in by providing two functions. The first is the bg, which stands for background. It's useful for starting a Coroutine on a background thread. Now this is something that we would do often when we're working with AsyncTask on Android.
And then the second function provided is the asReference extension method. This is useful if you're working with Coroutines that are spawned directly from Android View classes like activities or fragments, because they can help us to avoid leaking references to those objects. Here's an example of the background method at work. We get a list of our planets from the network on a background thread, and then we use them on the main thread. So this is a very common interaction on Android.
So as you can probably tell, Anko is somewhat of a mixed bag of tricks. But once you start working with the many extension functions and properties, you'll be hooked.
- 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