Xamarin.Forms is fantastic for quickly shipping a single code base to multiple platforms and devices. As soon as you are present on all those different screens, you need to make sure your UI looks as you expect, and even adapt the layout for your specific goals. FlexLayout makes this easier than before and provides new options for you to space and distribute your UI to suit different dimensions.

Let it Flow

Children placed in a FlexLayout container will flow in either a horizontal row or a vertical column, also called the main axis. This is set via the Direction property. Using this property alone, the container behaves very much like a StackLayout. Here is a login page that does this very thing.

The inner layout stacks a label and entry form vertically by setting the flex-direction css property to column.

Note: If CSS isn’t your preference, these properties are all available in XAML and C# as well. <FlexLayout Direction="Column" ...>.

In addition to column and row (default), you can also flip the order of the children by using “ColumnReverse” and “RowReverse”. Let’s see a StackLayout do that!

Notice how the login form is centered in the screen. This is something FlexLayout makes really easy by using align-items to center within the chosen direction axis (in this case the Y or vertical axis), and justify-content to distribute the children items to the center of the container (in this case along the other axis).

Pull down this sample from GitHub (https://github.com/davidortinau/flexibility) and try out those properties to see how the login view will change and adapt.

Wrap It To Go

Now that you see how FlexLayout will flow the content of your view, what about when your content overflows the width or height? This is when you can take advantage of wrapping. You can choose from several options:

  • wrap
  • nowrap
  • reverse

 

Let’s say your app has a grid of achievements like those displayed in the Conference Vision demo app from Build 2018. You populate the container with a series of the same item and want to make sure they wrap in a flow direction.

Just like you used justify-content in the previous example to center the login container within the view, you can use that property here to evenly space the content. Choose from:

Center Indicates that child elements will be grouped toward the center of the parent.
End Indicates that child elements will be justified to the end of the line.
SpaceAround Indicates that child elements will be spaced with one unit of space at the beginning and end, and two units of space between them, so that the elements and the space fill the line.
SpaceBetween Indicates that child elements will be spaced with equal space between units and no space at either end of the line, so that the elements and the space fill the line.
SpaceEvenly Indicates that child elements will be spaced to have the same space between each element as there is between the beginning and end elements and the nearest edges of the parent.
Start Indicates that child elements will be justified to the start of the line.

Source: Docs

Explore more of how FlexLayout is used in the Conference Vision app from the source on GitHub: https://github.com/microsoft/ConferenceVision.

Positioning In Space

Building on those examples, let’s explore a more advanced layout where you want to space content proportionally. Back in the Flexibility sample, there’s a photo page that wraps photos but also sizes them by percentage. Let’s say you want to have 3 images per row by default, but occasionally you want a full-width image, or images 2 per row? You can use the flex-basis property to see that by position.

Basis is how much space the item takes up relative to the main axis. In this case we are working along the X-axis (flex-direction: row). Basis may be expressed in device pixels or percentages, making this extremely powerful for responsive layout. Assigning the classes above to the images in the container, you can position them in this cool-looking grid.

For an additional example of using basis to achieve the “holy grail” layout, check out this Grid page sample.

Advanced Layout

FlexLayout can also be used for more advanced and inconsistent layouts such as this page. You can build on the same methods above to easily achieve this. Below is a layout that wraps rows and uses a trick to line things up. To force a line break, add an empty element with a basis of 100%.

 

There are a few other properties that will come in very handy as you start using FlexLayout, namely Grow and Shrink. Those properties do just what they say, sort of. Grow defaults to 0 and any positive number will take up that available space along the main axis. This is useful if you want something to consume available space left over by the siblings on that axis. Shrink, on the other hand, will do the opposite. Setting this to 0 will make sure it doesn’t shrink if siblings try to take up more space, whereas setting this to a positive number will allow that element to surrender space to other elements.

Notice the .year, .rating, .length elements have flex-grow: 1; which forces them to take up as much space as they can along that line evenly, giving it a nice spacing and pushing the popular stars to the left. Play around with those properties on these layouts and see how things adapt.

Adapting to Tablet Layout

When designing layouts for not only a variety of mobile screen dimensions but also tablets, you can get very far with the flexibility of FlexLayout and use of percentages to set the basis. There will come a time when you need to have a different layout that is slightly adjusted to suit another size of the screen, usually tablet or desktop. For this need, you can take advantage of OnIdiom. This little helper, similar to OnPlatform, let’s you specific divergent behavior or styling based on the idioms: desktop, phone, tv, and watch.

Take the first sample of the login page above, and consider how you could move the login form to the right of the screen on a tablet while introducing an image on the left.

In this layout, introduce a new Image just before the login form, and use the margin to provide a little space. To make sure this only appears on tablet, use OnIdiom to set the visibility accordingly.

Note: This OnIdiom syntax is new in Xamarin.Forms 3.2.0, so make sure to update to the latest pre-release in order to use that. For older example of OnIdiom check out the unit test sample here.

When using justify-content: center the FlexLayout will space things out for you nicely. This will make the layout look great!

Start Flexing Today With Xamarin.Forms

Update your projects to Xamarin.Forms 3.1.0 today via your Visual Studio NuGet package manager to enjoy the latest stable release with FlexLayout. And if you want to get all the newest improvements, check out the Xamarin.Forms 3.2.0 pre-release with that nice OnIdiom syntax and so much more.

Explore the samples used here and others created by the amazing docs team.