Zero to Kotlin hero: An introduction to the Anko library

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Let’s take a look at Anko library, the Beyonce of Kotlin Android libraries. Don’t judge me, it’s my opinion (lol).

The Anko library is an Android library written and maintained by JetBrains. Anko helps you to work faster and smarter when building Android apps.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

From what I’ve gathered, the above graphic is how the name Anko came about.

Anko has four parts:

  1. Anko Commons
  2. Anko Layouts
  3. Anko SQLite
  4. Anko Coroutines

To add the dependency for Anko to your project, simply go to your project-level​ build.gradle file and add this:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

You should add the latest stable Anko version to your project. At the time that this article was written, the latest version was 0.10.8.

After this, go to the app level build.gradle to add the Anko dependency.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

If you do not want the whole library, you can just install the modules that​ you need.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Anko commons

Anko commons is a lightweight library that contains a lot of helpers for the Android SDK.

Anko commons contains helpers for intents, dialogs & toasts, logging, resources, and dimensions.

Anko commons for intents

Anko Commons can be used for explicit or implicit intents.

With the regular Kotlin Android code, you can move from one activity to another (e.g., SecondActivity) by doing:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

With Anko, it’s done this way:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

That’s it!

You can also pass some data while launching intents:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

With normal Kotlin, the same command would be more verbose:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Anko reduced four lines to one line.

Let’s also try to open a link in an external browser. Using the default Kotlin code, we would have three lines:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Let’s call on the Anko magician to transform this. We now would have:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Anko has other wrappers for Intents; I will list them below. These methods return Boolean values. If the intent was sent, true is returned; if otherwise, false is returned.

If you want to make a call, you can simply call:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

If you want to send a text:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

If you would like to share text:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

If you would like to send an email:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Anko commons for dialogs and toasts

As we have established, Anko makes code shorter and more readable when talking about intents. When using the default syntax, we can launch toast messages like this:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

With Anko, however, we can launch the same message like this:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Anko also has a longToast() method if we want the duration of the toast message to be longer.

Similarly, if we want to show a snackbar using Anko, all we have to do is:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Showing alerts using Anko commons

Using the AlertDialog in Android can get messy when you have to call setTitle(), setMessage(), create(), etc.

By default, creating an alert dialog can be done using the syntax below:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

However, Anko made it easier for us by giving us the power to do this:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

The Anko version seems cleaner to me 🤷🏼‍♀️.

In the next article, we will cover logging, resources & dimensions. See you there!

Free Resources

Attributions:
  1. undefined by undefined