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.
Dependencies
Section titled “Dependencies”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.
Layouts in code
Section titled “Layouts in code”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.
Commons helpers
Section titled “Commons helpers”These helpers come from Anko Commons and are used in the layout above:
startActivity<DetailsActivity>("key" to "value")builds anIntentforDetailsActivity, puts eachPairin as an extra, and starts it.alert(message, title) { yesButton { ... }; noButton { ... } }.show()wrapsAlertDialog.Builder.toast("...")is short forToast.makeText(...).show().
The same screen in Jetpack Compose
Section titled “The same screen in Jetpack Compose”Compose replaces both the XML layouts and the Anko DSL:
import android.os.Bundleimport android.widget.Toastimport androidx.activity.ComponentActivityimport androidx.activity.compose.setContentimport androidx.compose.foundation.layout.Columnimport androidx.compose.material3.Buttonimport androidx.compose.material3.Textimport androidx.compose.material3.TextFieldimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.rememberimport androidx.compose.runtime.setValueimport 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.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)