var alertDialog = AlertDialog.Builder(this) .setMessage("Hi, this is a message for the AlertDialog.") .setTitle("Title of the AlertDialog") .setPositiveButton("OK", DialogInterface.OnClickListener { dialog, which -> doSomethingPositive() }) .setNegativeButton("Cancel", DialogInterface.OnClickListener { dialog, which -> doSomethingNegative() }) .create() .show()
alert(“Hi, this is a message for the AlertDialog.”, “Title of the AlertDialog”) { yesButton { doSomethingPositive() } noButton { doSomethingNegative() } }.show()
write the same code again. • It is not typesafe. • It is not null-safe. • No code reuse. • XML parsing is too much overhead. Takes too much CPU time, and hence battery.
a separate class where you can write your DSL code for your UI. - It is reusable. - It keeps the UI separate from logic. - You can convert existing XML files to AnkoComponents with the Anko Support plugin. - You also get a layout preview if you install the Anko Support plugin.