Creating user-interface programmatically
Writing UI in XML is not type safe or null safe, and it also eats CPU and battery. Writing UI programmatically (especially in Java) becomes bulky and unmanageable for large and complex UIs. That is when Anko layouts come to the rescue. We can easily create layouts in DSL using Anko layouts, and it also has no runtime overhead. In this recipe, we will see how to create layouts using DSL.
Getting ready
I'll be using Android Studio 3 to write code. You can get started by creating a new project in Kotlin with a blank activity in Android Studio 3+, as we won't be using any code from other recipes. You also need an intermediate understanding of Android development. Ensure that you have added Anko layouts dependencies to your project (follow the recipe Setting up Anko library for Anko layouts in gradle, in this chapter).
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...