Skip to content

Anko for Android: Kotlin layout DSL, intents and dialogs (deprecated)

Anko was a JetBrains library for writing Android apps in Kotlin. It had two main parts. Anko Layouts was a Kotlin DSL that built view hierarchies in code instead of XML. Anko Commons was a set of helpers for intents, dialogs, toasts and logging. It’s worth knowing if you maintain an older codebase that still uses it.

The last release was 0.10.8:

dependencies {
implementation 'org.jetbrains.anko:anko-commons:0.10.8'
implementation 'org.jetbrains.anko:anko-sdk27:0.10.8'
implementation 'org.jetbrains.anko:anko-appcompat-v7:0.10.8'
implementation 'org.jetbrains.anko:anko-design:0.10.8'
}

anko-commons has the helpers and anko-sdk27 has the layout DSL for the platform views. The appcompat-v7 and design artifacts target the old Android Support Library, not AndroidX, so an AndroidX project also needs Jetifier enabled.

With Anko Layouts you subclass AnkoComponent and build the view tree in createView:

class MainActivityUI : AnkoComponent<MainActivity> {
override fun createView(ui: AnkoContext<MainActivity>): View = with(ui) {
verticalLayout {
val name = editText {
hint = "Enter your name"
}
button("Say Hello") {
onClick { ctx.toast("Hello, ${name.text}!") }
}
button("Open Details Activity") {
onClick { ctx.startActivity<DetailsActivity>("key" to "value") }
}
button("Show Dialog") {
onClick {
alert("This is an Anko dialog", "Anko Dialog") {
yesButton { toast("You clicked Yes!") }
noButton { toast("You clicked No!") }
}.show()
}
}
}
}
}

The activity then sets the component as its content view:

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
MainActivityUI().setContentView(this)
}
}

Because editText returns the created EditText, you can hold a reference to it in a local val and use it in a click handler without calling findViewById. verticalLayout, editText and button are only available inside a layout DSL block like this one, not directly in an activity’s onCreate.

These helpers come from Anko Commons and are used in the layout above:

  • startActivity<DetailsActivity>("key" to "value") builds an Intent for DetailsActivity, puts each Pair in as an extra, and starts it.
  • alert(message, title) { yesButton { ... }; noButton { ... } }.show() wraps AlertDialog.Builder.
  • toast("...") is short for Toast.makeText(...).show().

Compose replaces both the XML layouts and the Anko DSL:

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
var name by remember { mutableStateOf("") }
val context = LocalContext.current
Column {
TextField(
value = name,
onValueChange = { name = it },
placeholder = { Text("Enter your name") },
)
Button(onClick = {
Toast.makeText(context, "Hello, $name!", Toast.LENGTH_SHORT).show()
}) {
Text("Say Hello")
}
}
}
}
}

For dialogs, use Compose’s AlertDialog. For starting activities, use a plain Intent or context.startActivity(Intent(context, DetailsActivity::class.java)). Android KTX’s bundleOf("key" to "value") replaces Anko’s pair-based extras.

© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)