Making Kotlin easier 😲 (Android development)

Anko is a library by Kotlin which makes Android development faster and easier while making the code clean and concise. There are a lot of simple things in Android app development that we use a very often like Intents, dialogs, toasts, layouts etc but they are very bulky in code. I mean 4–5 lines of code for a simple Intent and don’t even get me started on dialogs.

If like me, you have been writing custom methods for toasts dialogs and Logging, there is no need to add that boiler plate code in every project you create any more. Anko takes care of all of that.

Let us discuss how we can simplify and shorten the process of creating dialogs with Kotlin, mostly because the dialogs are the most unnecessary bulky ones in my opinion. And adding an XML layout for a every custom dialog we need, quickly adds a lot of files in your res/layout/ directory. So let us just get started.

Step 1: Add Anko commons dependencies in your build.gradle

dependencies {
compile "org.jetbrains.anko:anko-commons:$anko_version"

where $anko_version is the latest version of Anko release.

BTW, did you know you can now write your build.gradle files in Kotlin, why? Hello, auto-completion! Probably going to write about it next. Follow me to get latest updates :)

Step 2: Let us create a simple alert dialog

alert("Hi, I'm Rashi", "Do you find my posts helpful?") {
yesButton { toast("Thanks, will start writing more :)") }
noButton {
toast("Alright, let's start working on improvements") }

Here is how it looks:

And if you guys chose CANCEL :( there comes the toast:

That worked out nicely except your CANCEL button click. Anyways, usually I need to create custom dialogs in my projects. Is there a way to do that using Anko?

Well yes, how about we try the following:

alert {
= "Hi, Its Rashi again"
positiveButton("Cool") { toast("Yay :D") }
customView {
verticalLayout {
textView("Follow me for posts on tech, design and spirituality.")
text = "I'm in!"
onClick {
info{"thanks for following"}
padding = dip(16)

Did you notice what I did here. I basically showed a toast, Logged an info message and obviously showed an alert dialog with custom view using Anko. Pretty cool ha, but it won’t work for you until you add some more dependencies in your build.gradle as well. Why, because we are using Anko layouts. Writing layouts in DSL is so very simple as you just noticed above.

Without further chit chat, here are the dependencies:

// Anko Layouts
compile "org.jetbrains.anko:anko-sdk25:$anko_version"
compile "org.jetbrains.anko:anko-appcompat-v7:$anko_version"

// Coroutine listeners for Anko Layouts
compile "org.jetbrains.anko:anko-sdk25-coroutines:$anko_version"
compile "org.jetbrains.anko:anko-appcompat-v7-coroutines:$anko_version"

And here is our final result:

Assuming you clicked I’M IN! this time. Anyways there is a lot more in Anko that will help you shorten coding time and readability of your code. This article was a simple intro to what lies ahead. Don’t forget to learn more about Anko Layouts as there is a lot of more cool stuff there that I did not touch here.

Here are your links to start with Anko:



And your links to finally get to follow me:

> On Medium:

> On Twitter:

> Know more about me:

Author of Kotlin Programming Cookbook | Android Developer |

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store