Thursday, 3 August 2017

Nested View Group

When discussing how to position views in the last quiz, you may have mentioned using a relative layout. I know we did. But I want to introduce you to another way that you can build the layout for our app that would be more ideal, using nested viewgroups.

Nested viewgroups means putting viewgroups inside other viewgroups. That way you can build more interesting and complex layouts like these. Let's see how you would build up one of these layouts. Say, for example, you have a vertical linear layout with three views, an image view and two text views. What if you wanted to overlay some text on top of this image? Well, with a linear layout, you can't overlap views. But with a relative layout, you can, so we can replace this child with a relative layout instead. Then, we can put the image view inside this relative layout. This image view is now aligned to each edge of the parent relative layout. So, there you see an example of a relative layout that's contained within a larger linear layout. You can have many combinations of view groups within other view groups. For instance, you can have a relative layout as a root view for an app. That can contain a couple of views like an image view and two text views. You can also add a linear layout as a child of this broader relative layout. This linear layout itself can contain other children like two image views. You can nest as many view groups inside other view groups as you want. However, be careful because the more nesting that you do, the more expensive it will be for your app to lay out on the screen. The relative layout, in particular, is a very flexible layout but it can be very complex to calculate the position of each of these views relative to others. So it's recommended to not have too many layers of nesting within a relative layout. I pulled some examples from real life Google apps to show you some cases where it would be useful to use nested view groups. The Google Now app shows you cards of relevant information based on your current situation, like the current time or current location. If you like a certain team like the Red Sox, good choice, then it can show you the score for the latest game. To build out the layout for this card, you can imagine using a vertical linear layout because the information is displayed as rows. However, the first item in this vertical linear layout is not just a single text view, it's made up of multiple views. In fact, you would build this first item as a horizontal linear layout, made up of an image view and two text views. Same for the second item in this vertical linear layout. You would build it with a horizontal linear layout containing an image view and two text views.

 Another card in the Google Now app shows relevant stocks that you may be interested in. I wish the Google stock was still at that price. Anyways, you can also build out this layout as a vertical linear layout, because you can imagine the rows here. Except these middle entries aren't just a single text view, they're made up of multiple text views. For this line item relating to the Google stock price, you could use a horizontal linear layout and fill it with four text views. Same for the other stock prices as well. These horizontal linear layouts are children of this broader vertical linear layout. In the Google Play music app, you can, of course, listen to music.

 While this looks like a complicated layout to build, you can actually break it down into smaller view groups that you recognize. This pattern feels like a vertical linear layout with three different children. This first child can be built using a relative layout, because the views are relative to each other. The second child can also be built using a relative layout, because the views are overlapping each other. And this third child here looks like a horizontal linear layout made up of five different buttons. Again, we can use nested view groups to build out this screen. This is just one way to build out the layout. You can build it in many other ways as well. In the Google Maps app, earlier we saw that there are detailed pages for individual locations, like restaurants.

 You can't build out this screen with just a single linear layout or a single relative layout. You need to nest a bunch of view groups together. On a broad scale, we can see that the information is almost organized into rows. So, we can use a vertical linear layout as the parent view. One child could be this whole layout here. Another child could be this row of buttons. Another child would be this description and so on. But within each child is not just a single view. We have multiple views. This child in the broader linear layout is made up of three individual views. So you can use a horizontal linear layout with equal weights to each view so that they get spread out evenly. This child in the broader vertical linear layout can also be made up of a horizontal linear layout with an image view and a text view. If you keep scrolling this page, you see more information on the restaurant. Again, I can imagine the individual rows of information here. Using a vertical linear layout for all this information still works, and, again, some rows are more complex than just a single text view. This row shows a bunch of available times where you can make a reservation, and it's made up of three different buttons that you can arrange using a horizontal linear layout. This row of pictures can be built using a horizontal linear layout as well. And actually, within this horizontal linear layout, this first child is not just a single image view. It's an image view with a text view. So you could use a relative layout to build out this first child. So you could see how there can be view groups, within view groups, within view groups. There's actually more view groups beyond just relative layout, and linear layout, there's also frame layout and grid layout. We're not going to have time to cover those in this course, but I definitely recommend that you go out and learn about them on your own because sometimes those would be better than just using a relative or a linear layout.

No comments:

Post a Comment