Kotlin — Anko & the commons

Alejandro Moya
Codelitt
2 min readMay 9, 2019

--

In the previous posts I explained some goodies from kotlin, like replacing listeners and view binding, things that help us be more productive. In these next posts I’m going to talk about another library from the kotlin team, Anko. Anko is a toolbox full of methods and extensions that make Android development using kotlin so easy that you’ll think your time using java was a real nightmare. We need to split these in a few posts due to the amount of tools available.

We’ll start with ”the commons” and don’t confuse this with some good ol’ music band 🎼, these are the basic tools included with the library.

First the easy step, adding anko to the gradle file:

dependencies {
implementation “org.jetbrains.anko:anko:$anko_version”
}

where $anko_version will be the version number available from anko, you could create a parameter or just replace the parameter with the version number. This will include all of anko’s libraries, you could choose to use only one or more of those, they’re shown on the readme in the github repo.

Let’s begin with some dialogs:

  • We want to display a toast
toast(R.string.myToast)//ortoast("My toast")//orlongToast(R.string.myToast)
  • A Snackbar?
snackbar(…)//orlongSnackbar(…)//orsnackBar(…, “click here”) {
//code to be executed on click
}
  • An Alert?
alert(“title”, “message”) {
yesButton { //yes snippet }
noButton { //no snippet }
}.show()
  • with Appcompat
alert(Appcompat, “message”).show()

You can customize the alerts and there’s also methods for selectors and progress dialogs.

Intents

  • A phone call?
makeCall(“555–555–5555”)
  • Browse url?
browse(“https://www.codelitt.com")
  • Start an Activity?
startActivity<MyActivity>(“extra_param1” to 1, “extra_param2 to “data”)//with flagsstartActivity(intentFor<MyActivity>(“extra_param1” to 1).noHistory())

One more important set, Async!

(Context).runOnUiThread {
//code to be executed on the UI thread
}
doAsync { //it receives the caller as the ‘it’ parameter
//code to be executed asynchronously
}

Also on async there are methods to run on the UI thread with different options, receiving the context in the execution block, from an Activity, and more.

With the previous examples you will reduce the amount of code needed, also the simplicity is huge, if you look at the library code, many of the functions are inline which reduces the final method count.

We also have tools for measurement like the dip() and sp() methods that accept an Int or Float number.
Methods that run blocks if the sdk version is equal-below-above the Int parameter supplied.
Extensions for collections with some handy methods.

You can go to this path in the Github repository for more info.
Some of the methods are deprecated because they are being replaced by KTX, a library that has a similar meaning but developed by Google (we will talk about it in the Jetpack post in a near future).

In the next post we are going to dive into the co-routines, stay tuned for more anko.

--

--

Alejandro Moya
Codelitt

Mobile developer, Father, geek, this is my first attempt at having a blog, be kind 😄