How to do it…

Let's start with a simple example where we use Anko to create a layout for our target activity (the activity in which you want to create a layout):

  1. Here's the code for the onCreate() method that you need to put in your target activity:
override fun onCreate(savedInstanceState: Bundle?) {    super.onCreate(savedInstanceState)    verticalLayout {        padding = dip(20)        val name = editText {            hint = "What is your name?"        }        val message = editText {            hint = "Your message"        }        button("Send") {            onClick { toast("Hello, ${name.text} we have recorded your message!") }        }    }}
  1. Basically, in the preceding code, we want to create a basic "contact us" form. For this, we have created a vertical linear layout with a 20 dip padding and, inside the vertical ...

Get Kotlin Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.