Advanced Features of Anko

Last week we published a new version of Anko. While the main purpose of this library is creating layouts though a DSL, even the users of XML layouts can benefit from it. Today we are going to talk about such “ambivalent” features of Anko.

Intent Helpers

The common way of starting a new Activity is to create an Intent, maybe put some parameters into it, and finally pass the created Intent to the startActivity() method of a Context.

With Anko we can do this in exactly one line of code:

startActivity() function accepts key-value pairs that will be passed as Intent extra parameters. Another function, startActivityForResult() with similar semantics is also available.

Please refer to the Intent Builder Functions reference section for more information.

Popular Intent Shorthands

Almost every application has code that loads a page in the default web browser or opens a new email screen using Android intents, so there are helper functions for this in Anko:

Other useful intents are described under the Useful Intent Callers reference section.

Alert Dialogs

Anko provides a declarative way of creating alert dialogs with text messages, lists, progress bars and even with your own DSL layout.

For a simple text alert with a couple of buttons at the bottom, all you need is:

There is a function that creates and shows a list dialog:

Both indeterminate and basic progress dialogs are supported:

Also, as mentioned above, you can use Anko’s DSL in dialogs to create a custom layout:


Android system services, such as WifiManager, LocationManager or Vibrator, are available in Anko through extension properties for the Context:

Asynchronous Tasks

Probably the most popular way to execute code in the background is to subclass an AsyncTask. But, despite of its popularity, it is inconvenient in many ways. Anko has several functions which practically do the same but are easier to use.

async() {...} function executes code inside {} under the ThreadExecutor. You can use the default one or pass your own.

If you want to go back to the UI thread inside async(), you can use uiThread() function.

uiThread() has a special semantics inside async(): async() does not hold a Context instance but only a WeakReference to it, so even if lambda execution never finishes, the Context instance will not leak.


Android SDK provides the android.util.Log class which consists of a few logging methods. Usage is straightforward but the methods require passing a tag argument, and the actual log message must be a String. You can get rid of this by using the AnkoLogger trait:

The default tag name is a class name (SomeActivity in this case), but you can easily change it by overriding the loggerTag property of AnkoLogger.

Each method has two versions: plain and “lazy” (lambda will be executed only if Log.isLoggable(tag, Log.INFO) is true).

You can read more about logging in the Logging reference section.


To try Anko, follow these instructions.

And as usual, your feedback is very welcome.

About Andrey Breslav

Andrey is the lead language designer of Kotlin at JetBrains.
This entry was posted in Android. Bookmark the permalink.

3 Responses to Advanced Features of Anko

  1. cypressious says:

    Which version of Anko is required for the Intent helper methods?

  2. Pingback: Anko for Android « Otaku, Cedric's blog

Comments are closed.