In order to work successfully with Anko Layouts, you need to understand its use of the Anko Context and AnkoComponents. In this video, learn how the two work together to create DSL based layouts for your Android Views.
- [Instructor] In order to work successfully with Anko layouts, you need to understand its use of the AnkoContext and Anko components. First, you can think of the AnkoContext as the container for all of your Android goodies. It's an interface that holds a reference to the context, which is needed to create views programmatically, the owner of the created views, and the view itself. Notice that it also implements another interface, the ViewManager. The ViewManager contains a few methods for working with views, the most important of which is the addView method.
This takes in a view you want to add, and then the LayoutParams, which describe how this view should be positioned in its parent. And that's it for the AnkoContext. Next up is the Anko component. This is where we define what views we want to have created. This can be anything, from an entire screen's layout to just a simple component. And these views are defined using Anko's DSL. DSL, as a refresher, stands for Domain-Specific Language.
It can be thought of as a mini-language designed for some specific business need or context, like HTML, which is built for web pages, or SQL for database access and so on. Here's an example of that DSL. Now, let's break this down. First, let's notice the main views that we'll be creating. A verticalLayout, which is short for a linear layout with vertical orientation, then inside of it will be two child views, a textView, and a button.
And in fact, behind the scenes, this is more or less what's being generated for you, but because of Anko's DSL, you get to use a more streamlined API to create your views. If you've ever worked with something like Ruby Builders before, it's a very similar concept. Moving on to the next bit of code here, we see that we can further customize the views that we add through additional methods. First, on our textView, we can set the text that we want to display, as well as the textSize.
Therefore a button's the same thing. You can control the text. Anko provides convenience functions for the most common operations on Android views. And finally, you can even specify the layout information by means of the lparams function. And this can include things like margins and gravity. This DSL is placed inside of AnkoComponent, which itself, is just an interface with one method, createView. It takes in an AnkoContext, and then returns a view.
So, when you want to create views using Anko, you do so by implementing this method, and providing the desired layouts via Anko's DSL. The end result is a simple UI that can be expressed fluently in code and can be reused elsewhere if needed.
- What Kotlin has to offer
- Working with lambdas
- Common Android extension functions
- Kotlin Android Extensions overview
- Making use of the Anko library
- Working with coroutines
- Nullability and collections