A Peek Behind the Curtain6:01 with Ben Deitch
In this video we start looking at what Anko's doing behind the scenes!
We've just used the magic of Anko to create our entire layout and 0:00 side our activity. 0:04 But what's really going on behind the scenes here? 0:05 To get a better look let's get rid of everything inside a relative layout, 0:09 and then let's take a deep dive into the inner workings of Anko, 0:15 starting with this relative layout function. 0:19 Let's start by getting rid of the brackets, as well and 0:21 then adding parentheses so we can see what the parameters really are. 0:26 The first parameter is an integer representing which theme we're using. 0:31 And it defaults to zero. 0:35 We won't be using a theme for this app, so we'll be omitting the theme parameter. 0:37 The second parameter is a function named init. 0:43 This is the code that will be used to initialize the relative layout. 0:46 And it will be an extension function on the _relativeLayout class 0:50 which returns nothing. 0:54 Wait, did he say _relativeLayout? 0:57 Sure did, on the next line let's type underscore, relative layout. 1:00 And then use command or control + B to go to the declaration. 1:07 Here we can see that underscore relative layout 1:11 extends the regular relative layout class. 1:14 It then goes on to declare a few different versions of the l params function. 1:18 Let's take a look at the second one. 1:28 First off if you're not familiar with generics. 1:31 That's what's going on here with this letter T. 1:34 In this function T can be any class that extends view. 1:38 So lparams is an extension function on any class that extends view. 1:43 And inside this function, first we create the layout params 1:49 by using the width and height attributes which defaults to wrap content. 1:54 Then, we initialize those layout params using the init function we passed in 2:00 and next, we add those layoutParams to our view. 2:06 And remember this is of type t which extends view. 2:11 And finally, we return the view. 2:16 Cool, right? 2:19 This whole class is just to make it so that we have an easier time 2:21 of setting layout params for the views and our relative layouts. 2:24 In fact, the point of this entire file 2:29 is just to add the L primes extension function to the views, and our layouts. 2:32 It's just a bunch of different underscore layout, extends layout. 2:38 And then all the different l params functions for that layout. 2:46 Also I don't think I've mentioned it yet but 2:52 classes in Collin are closed by default. 2:54 So if you want to be able to extend the class, 2:58 you need to declare that class as open. 3:00 Okay, now that we know that underscore relative layout is just a relative layout 3:04 with some extra lparams functions, let's get back to main activity and 3:09 the relative layout function. 3:14 Let's delete the underscore relative layout line and then let's take another 3:17 look at the parameters to this relative layout function. 3:21 It looks like there is one version of this function with only a theme as a parameter 3:25 and another version that takes on both a theme and an init function. 3:30 And it looks like each of these versions is declared twice. 3:35 What's up with that? 3:39 Let's use command or control + B to jump to the declaration and 3:41 see what's happening. 3:44 And here we can see that both versions of our relative layout function, 3:47 the one that takes in only a theme and the one that takes in both a theme and 3:51 an init function, are declared as extension functions on the activity class. 3:55 But if we double click on relative layout and then use command or 4:00 control + F to find each occurrence. 4:04 It becomes clear that both versions of our relative layout function are declared as 4:08 extension functions on each of the view manager, context, and activity classes. 4:13 So since main activity is both an activity and 4:19 a context because the activity extends from context, 4:22 when we call the relative layout function it could be any of these four functions. 4:27 Okay, now let's dig a little bit deeper. 4:33 What actually happens when we call this function? 4:36 And since these are the same two functions let's just look at the activity ones. 4:39 First we've got the version that only takes in a theme. 4:49 This is the version we're currently using. 4:53 When we call relative layout without passing in any parameters 4:55 we're calling this function which gives us a theme of zero. 4:59 And then calls the second relative layout function while passing in that theme and 5:03 an empty init function. 5:08 The second relative layout function 5:10 then takes a relative layout constant along with our theme and 5:13 an if function and passes them on to a new function called ankoView. 5:18 But before we move on to Anko view, let's look at that relative layout constant. 5:23 Let's use command or control + B to jump to its declaration. 5:28 And here we can see that relative layout is just an anonymous function 5:34 which takes in a context and returns an instance of underscore relative layout. 5:38 Which remember, is just a relative layout with some extra lparams functions. 5:43 All right, now let's scroll back down to the relative layout function, And in 5:48 the next video, we'll finish our deep dive by jumping into the Anko view function. 5:55
You need to sign up for Treehouse in order to download course files.Sign up