Stack And Grid Layouts In Xamarin.Forms

By reading this, you will be able to make stack and grid layouts in Xamarin.Forms.

Let's start by defining what stack is

If you have some knowledge of data structures, you will know that stack is something to store data. Stack works in both a horizontal and vertical way.

xamarin

Here, you see both stacks in a horizontal and vertical way. Now, implement this stack in Xamarin.Forms.

  1. <StackLayout>  
  2. <Label Text="Enter Your Email"></Label>  
  3. <Entry Placeholder="Email"></Entry>  
  4. <Label Text="Enter Your Passowrd"></Label>  
  5. <Entry Placeholder="Password"></Entry>  
  6. <Button Text="Submit"></Button>  
  7. </StackLayout>   

By default, stack layout orientation is set to vertical, you can change it to horizontal if needed. Here, you can see both Android and Windows desktop implementation of this code.


xamarin

xamarin

This is the most simple and easy way to make login form using Microsoft XAML in Xamarin.forms.

Now, change the orientation of stack layout to horizontal and see how we can utilize this in our application.

You can use Orientation="Horizontal" to change its orientation.

  1. <StackLayoutOrientation="Horizontal">  
  2.     <Button Text="Section 1" BackgroundColor="Gray"></Button>  
  3.     <Button Text="Section 2" BackgroundColor="Navy"></Button>  
  4.     <Button Text="Section 3" BackgroundColor="Gray"></Button>  
  5.     <Button Text="Section 4" BackgroundColor="Navy"></Button> </StackLayout>   

In Android and in Windows.

xamarin

xamarin

Other properties of stack layout include spacing of layout.

E.g. Spacing="20" 

You can give both integer and decimal value in spacing.

Try it for your practice :)

Now, let's move towards Grid Layout.

What is Grid?

In Grid layout, you can divide your screen in rows and column to make your layout more attractive. Grid layout is mostly used in photo Gallery and others things where Grids are used.

Grid is something like this.

xamarin

Let's try its example by using XAML

Steps to remind while making a Grid View.

  1. Define Total rows 
  2. Define Total Columns 
  3. Define all elements you want to place in your layout and set rows and columns needed by this element.

Code Example

  1. <GridRowSpacing="5" ColumnSpacing="5" BackgroundColor="Black">  
  2.     <Grid.RowDefinitions>  
  3.         <RowDefinitionHeight="*" />  
  4.         <RowDefinitionHeight="*" /> </Grid.RowDefinitions>  
  5.     <Grid.ColumnDefinitions>  
  6.         <ColumnDefinitionWidth="*" />  
  7.         <ColumnDefinitionWidth="*" /> </Grid.ColumnDefinitions>  
  8.     <ImageSource="http://bit.ly/2oI1MKW" Row="0" Grid.Column="0" BackgroundColor="White">  
  9.         </Image>  
  10.         <ImageSource="http://bit.ly/2oI1MKW" Row="0" Grid.Column="1" BackgroundColor="White">  
  11.             </Image>  
  12.             <ImageSource="http://bit.ly/2oI1MKW" Row="1" Grid.Column="0" BackgroundColor="White">  
  13.                 </Image>  
  14.                 <ImageSource="http://bit.ly/2oI1MKW" Row="1" Grid.Column="1" BackgroundColor="White">  
  15.                     </Image>  
  16.                     </Grid>   

 Just like stack layout spacing, In Grid layout, you can set both row and column spacing .

  1. <Grid.RowDefinations>  
  2. </Grid.RowDefinations>    

This tag is used to define total number of rows and similarly, <Grid.ColumnDefination> tag is used to define all columns in Grid.

Here, this Grid looks like,

xamarin

xamarin

These black lines are due to spacing between rows and columns and background color of Grid is set to black.

Thanks for reading.


Similar Articles