Skip to content

Learn Mobile Development

Alamgeer's Blog

Menu
  • Home
  • Privacy Policy
Menu
FlexLayout in MAUI

How to use FlexLayout in MAUI

Posted on December 2, 2022January 18, 2023 by Learn Mobile Development

FlexLayout in MAUI is a layout that can arrange its children horizontally and vertically in a stack. It is also wrap its children if there are too many children to fit in a single row or column. FlexLayout can control orientation and alignment, and adapt to different screen sizes.

In this article, I’m going to show you how to use FlexLayout in MAUI to add skills.

Let’s Start

1 – Setting up the UI

1.1 – MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:converter="clr-namespace:MAUIFlextLayout"
             x:Class="MAUIFlextLayout.MainPage">

     <ContentPage.Resources>
            <converter:StringToReverseBoolConverter x:Key="IsLableShow" />
            <converter:StringToViewSizeStringConverter x:Key="ViewSizeConverter" />
    </ContentPage.Resources>

    <ScrollView Padding="0,0,0,20">
        <VerticalStackLayout
            Spacing="15"
            Padding="20,20"
            VerticalOptions="Start">

            <Image
                Source="dotnet_bot.png"
                SemanticProperties.Description="Cute dot net bot waving hi to you!"
                HeightRequest="200"
                HorizontalOptions="Center" />

            <Label Text="Skills : "
                   FontSize="20"
                   Margin="5,0,0,0"
                   FontAttributes="Bold"/>

            <FlexLayout
                    x:Name="FlexSkillContainer"
                    Margin="5,-10,5,5"
                    AlignContent="Start"
                    AlignItems="Start"
                    HorizontalOptions="FillAndExpand"
                    VerticalOptions="Fill"
                    Direction="Row"
                    JustifyContent="Start"
                    BindableLayout.ItemsSource="{Binding Skills}"
                    Wrap="Wrap" >

                   <BindableLayout.ItemTemplate>
                        <DataTemplate>
                             <Frame
                                Margin="2"
                                Padding="4"
                                BackgroundColor="{StaticResource ColorFocused}"
                                CornerRadius="15">
                                   <StackLayout
                                        Margin="5,0,5,0"
                                        Orientation="Horizontal">

                                            <Label
                                                x:Name="LabelSkill"
                                                FontSize="18"
                                                Text="{Binding .}"
                                                Margin="5,0,5,0"
                                                TextColor="{StaticResource ColorWhite}"
                                                VerticalOptions="Center" />

                                            <Image
                                                x:Name="ImgCross"
                                                HeightRequest="24"
                                                HorizontalOptions="End"
                                                Source="cross.png"
                                                VerticalOptions="Center"
                                                WidthRequest="24">

                                                <Image.GestureRecognizers>
                                                    <TapGestureRecognizer Tapped="OnDeleteSkillClicked" />
                                                </Image.GestureRecognizers>

                                            </Image>

                                 </StackLayout>
                             </Frame>
                        </DataTemplate>
                   </BindableLayout.ItemTemplate>

             </FlexLayout>

            <FlexLayout Direction="Row" HeightRequest="50">

                <Entry
                    x:Name="SkillEntry"
                    Margin="5"
                    FlexLayout.Basis="{Binding Source={x:Reference SkillEntry}, Path=Text, Converter={StaticResource ViewSizeConverter}}"
                    FontSize="20"
                    HeightRequest="50"
                    VerticalOptions="Fill"
                    HorizontalOptions="FillAndExpand"
                    Keyboard="Text"
                    Placeholder="Add Skill" />

                <Image
                    x:Name="ImageCheck"
                    FlexLayout.Basis="10%"
                    HeightRequest="35"
                    FlexLayout.AlignSelf="Center"
                    VerticalOptions="CenterAndExpand"
                    IsVisible="{Binding Source={x:Reference SkillEntry}, Path=Text, Converter={StaticResource IsLableShow}}"
                    Source="check.png"
                    WidthRequest="35">

                    <Image.GestureRecognizers>
                        <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
                    </Image.GestureRecognizers>
                 </Image>

            </FlexLayout>

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>
  • As in the above MainPage.xaml I have used two FlexLayout first FlexLayout has used for showing the list of skills and second FlexLayout contains ‘Entry’ and ‘Image’.
  • I have used below properties in FlexLayout:
  1. Direction=”Row” => Children will appear horizontally.
  2. JustifyContent=”Start” => Children will aligned from start without any spacing.
  3. Wrap=”Wrap” => FlexLayout will wrap its children. If there are too many children to fit in a single row.
  • In the second FlexLayout i have used FlexLayout.Basis attached property to its children.
  • FlexLayout.Basis, defines the amount of space that’s allocated to a child on the main axis.
  • For Entry i have used StringToViewSizeStringConverter converter to manage the width size.

2 – Create a StringToViewSizeStringConverter.cs converter

using System;
using Microsoft.Maui.Layouts;
using System.Globalization;

namespace MAUIFlextLayout
{
    public class StringToViewSizeStringConverter : IValueConverter
    {
        public StringToViewSizeStringConverter()
        {
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string val = (string)value;

            if (!string.IsNullOrEmpty(val))
            {
                return new FlexBasis(0.90f, true);
            }
            else
            {
                return new FlexBasis(1f, true);
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return !((bool)value);
        }
    }
}

  • As in the above StringToViewSizeStringConverter.cs If Entry text length is more than 1 then StringToViewSizeStringConverter converter will return 90% of FlexBasis else 100% FlexBasis.
  • And I have used another StringToReverseBoolConverter converter to Image for hiding and showing.

3 – Create another StringToReverseBoolConverter.cs converter

using System;
using System.Globalization;

namespace MAUIFlextLayout
{
    public class StringToReverseBoolConverter : IValueConverter
    {
        public StringToReverseBoolConverter()
        {
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string val = (string)value;

            if (!string.IsNullOrEmpty(val))
            {
                if (val.Length > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return !((bool)value);
        }
    }
}
  • If Entry text length is more than 1 then image icon will be visible else not visible.

4 – Manage Add/Delete skills in MainPage.xaml.cs

using System.Collections.ObjectModel;
using System.ComponentModel;

namespace MAUIFlextLayout;

public partial class MainPage : ContentPage, INotifyPropertyChanged
{
    private ObservableCollection<string> _skills;
    public ObservableCollection<string> Skills
    {
        get
        {
            return _skills;
        }
        set
        {
            _skills = value;
            OnPropertyChanged("Skills");
        }
    }

	public MainPage()
	{
		InitializeComponent();
        Skills = new ObservableCollection<string>();
        BindingContext = this;
    }

    void OnAddSkillClicked(System.Object sender, System.EventArgs e)
    {
        Skills.Add(SkillEntry.Text);
        SkillEntry.Text = "";
    }

    void OnDeleteSkillClicked(System.Object sender, System.EventArgs e)
    {
        string skill = (sender as Image).BindingContext as string;

        if(!string.IsNullOrEmpty(skill))
        {
            Skills.Remove(skill);
        }
    }
}

Now, We have completed the coding, Let’s try to run the application.

5 – Result

That’s all for now!

You can check the full source code here.

Happy Coding! 😀

You may also like

UniformItemsLayout in MAUI

How to use UniformItemsLayout in MAUI

Expander in MAUI

How to use Expander in MAUI

Post Views: 301
Share article with

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

I'm Alamgeer Ashraf and having 9+ years of experience in enterprises mobile application development with Xamarin Native, Xamarin Forms & .Net MAUI.

Archives

  • February 2023 (1)
  • January 2023 (9)
  • December 2022 (5)
  • November 2022 (6)

Latest Posts

  • Prevent Dark Mode in Xamarin forms
    How to Prevent Dark Mode in Xamarin formsFebruary 3, 2023
  • apply color to Images
    How to apply color to Images and Icons in MAUIJanuary 30, 2023
  • iOS Large Page Title
    How to set iOS large page title in MAUIJanuary 26, 2023
  • Change StatusBar Color
    How to change StatusBar color in MAUIJanuary 22, 2023
  • Item reordering to CollectionView
    How to apply item reordering to CollectionView GridItemsLayout in MAUIJanuary 18, 2023

Popular Posts

  • How to apply color to Images and Icons in MAUI
  • How to use DockLayout in MAUI
  • How to use Expander in MAUI

Latest Comments

  1. Randyperly on How to use StateContainer in MAUIOctober 18, 2024

    very good _________________ netgame casino бездепозитный бонус

  2. SmiSew on How to use StateContainer in MAUIOctober 10, 2024

    Thanks for the post _________________ pin up casino бест

  3. StavkiTarve on How to use StateContainer in MAUIOctober 7, 2024

    interesting post _________________ слоты в 1xbet приложение

  4. Luke on How to use drag and drop gesture to CollectionView in MAUISeptember 25, 2024

    Thanks very much!

  5. Tony - Nhan Nguyen Lhoa Minh on How to apply item reordering to CollectionView GridItemsLayout in MAUIMarch 28, 2024

    That would be an amazing solution. It's easy to comprehend. Thank you very much.

Our Visitor

0 0 7 9 2 0
Users Today : 3
Users Yesterday : 1
Users Last 7 days : 24
Users Last 30 days : 122
Users This Month : 4
Total views : 14162
How to change StatusBar color in MAUI
Trending
How to change StatusBar color in MAUI

Category

  • .Net MAUI
  • MAUI Community Toolkit
  • MVVM
  • Xamarin Community Toolkit
  • Xamarin Forms

Contact Me

  • LinkedIn
      © 2025 Learn Mobile Development