 Hello everyone, I hope you're all doing well This week's lab is going to be focused on views and view groups So we're continuing along plowing through our book at break-necking pace Getting closer to our final destination of total control over the Android UI We'll get there and then once we're after that We'll talk about design principles and some of the cooler back-end stuff of working with the cloud and once we get there I'll be doing a lot more hands-on stuff with you, but for now. I think the book's doing a pretty good job I do want to introduce this topic and I want to talk about just a few kind of key concepts You're going to run into As well as something that may have been tripping you up so far on your labs and on your assignment Okay, so recall This is all about views and view groups in this chapter If you recall views are the basic building block for user interface components and in Android a view occupies a rectangular area on the screen and The view is also a class and it is responsible for drawing and handling events within its little rectangular box. Okay so views Thus far have been specified in these XML layout files that accompany your activity And what happens is under the hood Android reads these XML files that you create for your layouts for your activities and converts them all into Java objects that subclass this Java view class and Again as a reminder, why do we do that or why did Android elect to do that? basically because writing GUI code in Java is very painful and Excuse me putting it inside an XML file and kind of automatically translating it is there for you to be more productive right so the view group is a subclass of the view and Its job is to arrange things and organize on the screen. Okay, so a view group can contain other views which are its children and It is the base class for layouts and other containers, right? And so far you've been interacting predominantly with the linear layout at least if you're following your book If you've been working with Android studio though You may have worked with or certainly seen this concept of a constraint layout and we'll get Next week is going to be focused on the constraint layout and really getting you up to speed on that So thus far you've seen very primitive things, right? This week you're going to get introduced to a whole bunch more stuff everything up here That's bold-faced and starred. This is new for you So by the end of this chapter you're going to be looking making things that are a lot more flexible And once you combine these new view types with say some pretty spiffy styling, you know Just with colors and rounded edges and kind of other things, you know You can tweak your fonts and things like that Once you get into that you'll have the ability to really make your UI start looking better In this chapter you get toggle buttons switches checkboxes radio buttons images You're going to learn how to put images inside your app Make it a button and you will learn about two layouts One is the frame layout for stacking things one on top of the other and the other is the scroll view So whenever you've got too much stuff on one screen and you just want to have scrolling You need to wrap that stuff in the scroll view and also the toast right the toast in Android if you've had Have an Android phone. You've certainly seen a toast. That's the little pop-up stuff that comes on the screen Okay chapter 5 is also going to give you some more tools to get your views Looking the way you want You'll talk about scaling your views relative to one another You'll learn about the concept of gravity and not Newton's gravity, but Android's gravity there's two notions of gravity one is Poorly named I should add one is just called gravity And that's how things are centered inside a widget. Do you want them on the right the left? Where do you want them? And then there's the notion of layout gravity, which is when you've got widgets Inside a layout or a view group. Where do those widgets go, right? So if you got multiple buttons, how do you get the buttons centered and aligned in the center? Versus that's layout gravity So aligning the widgets that are children of a layout and then there's regular gravity Which is aligning the contents of a widget within itself within that square So like where do you want the text on your button to be do you want it to be left justified? Right justified in the center. So that's the difference between gravity and layout gravity. You'll get into that more about orientation of your contents margin padding and then an interesting concept called start and end right for positioning things normally when you talk about positioning things in a layout you think in terms of top bottom right left You'll learn about start and end in this one. That's a very interesting case because some people in some countries with some languages read right to left and They expect the start of their content to be on the right hand side of the screen instead of the left hand side of the screen Android kind of takes care of that for you magically as long as you specify your layouts correctly Alright, you'll be adding pictures with an image view and stacking UI elements with the frame layout So this is not specifically in the lab for this week But it's something I want to bring up with you So you hopefully have a better idea of what's going on in that on create method in all you've your activities so here's an excerpt from the beer expert and I've tweaked this a little bit because I want to demonstrate a point with it Okay, so on create you've read your activity lifecycle chapter and you've done the work and you've read the official Google documentation on it and you know that on create is called whenever Android is getting ready to it's like building up It's like the constructor for your class and you got to initialize your data and everything Inside the on create method usually the first call is this Super dot on create saved instance state and you learned about that in a previous chapter Now there's a couple other things that you are frequently doing inside your activity class So what I have here on this line is Text view brands Okay, gets find view by ID r dot ID dot brands now. What does this do? Right the intention is we talked about find view by ID before and you got to understand kind of what this method does at least at a high level it goes to To your layout XML file which Android has converted into widgets for you And it says alright get the widget with this ID when you specify the widget in the Layout XML file you probably created an Android colon ID Attribute and it had like plus at ID slash Something right I called this one brands It goes and it finds that widget the actual Java object and assigns it here But my question to you is right now after this line of code executes What is the value of brands? Well the answer is it's null Okay, the reason why it is null at this point This is obviously not what you want right you want the text view there Maybe you want to set the text contents or get the contents out, but you clearly don't want this to be null That's bad. Why is why would it be null though? Go try it. You'll see that it is null Because whenever your activity is created Okay, whenever you're fine to be your activity is created It initially has no idea What its layout is or what should be in it right? It's just an empty Java class and it's kind of waiting for instruction But at the beginning of every on create method, you know Probably like right at almost right after you do super dot on create saved instance state You have a method that looks like this set content view R dot layout dot activity find beer. So what do you think this does? This is the magic connection between your activity and It's layout file right so we talked briefly about the view and the view model in several videos ago the view is The layout file it specifies all the widgets the view model is the activity it controls the behavior of the activity on the screen But whenever the activity is initially created it doesn't know What its layout file is and nowhere is it specified right? It's not magic that it finds it It is through this call Right here right this call is saying hey set the content for this activity To be this layout file r dot layout dot activity find beer Now this is a little bit of magic Android will map this name right here activity underscore find underscore beer to R dot layout dot activity find beer, okay This is the connection Once these connection is made, okay through the set content view call now Find beer activity can find its widgets Okay, and vice versa. Actually. I don't know if Right and vice versa now that like the widgets that are in here know where like on-click listeners are and things like that So now when you get down here brands gets fine view by ID, aha now this will process okay So you got to know You don't necessarily have to know deeply how they work though It's kind of interesting But you really have to know what these methods do the set content view and the fine view by ID What their parameters are what they've returned, okay? So what do you work on there's a lab for chapter 5 in your book? You're gonna go through that and assignment 2 is now posted So it's a assignment to builds upon assignment one. You're gonna take your existing code and just add on to it Alright, let me know what questions you have and good luck