Skip to content

Learn Mobile Development

Alamgeer's Blog

Menu
  • Home
  • Privacy Policy
Menu
Rotate views on orientation change

How to rotate views on orientation change in Xamarin Forms

Posted on November 20, 2022January 18, 2023 by Learn Mobile Development

It is very important to handle the pages on both orientation Landscape and Portrait to improve the user experience. So we need to adjust the existing view (without recreating it) in such a way that looks good in Landscape and Portrait mode. Xamarin forms handle orientation changes by default. So In this article, I’m going to show you how to rotate views on orientation change without recreating layout.

Let’s Start

To create such a type of page in xamarin forms we will use FlexLayout and VisualStateManager.

What is FlexLayout?

FlexLayout in xamarin forms is a layout which is similar to StackLayout. FlexLayout can arrange its children either horizontally or vertically in the stack. It is also capable of wrapping its children if there are many children to fit in a single row or column. You can learn more about FlexLayout here.

What is VisualStateManager?

VisualStateManager in xamarin forms is a way to make visual changes to UI from code behind. It introduces the concept of states. Let’s take an example of a button to explain states, Button has Normal state, Disabled state and Pressed state. We can also create custom states. You can learn more about VisualStateManager here.

In this sample demo I will create two custom states that are Portrait state and Landscape state.

1 – Setting up the UI

1.1 – Create a MainPage.xaml

Rotate views on orientation change
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="XFHandleViewOnOrientationChangeSample.MainPage">

     <ContentPage.Resources>
        <ResourceDictionary>
            <Style TargetType="FlexLayout">
                <Setter Property="VisualStateManager.VisualStateGroups">
                    <VisualStateGroupList>
                        <VisualStateGroup x:Name="OrientationState">

                            <!--  Portrait  -->
                            <VisualState x:Name="Portrait">
                                <VisualState.Setters>
                                    <Setter Property="Direction" Value="Column"/>
                                </VisualState.Setters>
                            </VisualState>

                            <!--  Landscape  -->
                            <VisualState x:Name="Landscape">
                                <VisualState.Setters>
                                    <Setter Property="Direction" Value="Row" />
                                </VisualState.Setters>
                            </VisualState>

                        </VisualStateGroup>
                    </VisualStateGroupList>
                </Setter>
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources>

    <ContentPage.Content>

         <FlexLayout
            x:Name="FlexLayout"
            Direction="Column"
            Wrap="NoWrap">

            <Image
                x:Name="Image"
                Aspect="AspectFill"
                FlexLayout.Basis="40%"
                Source="img.jpg"/>


            <StackLayout Margin="10" FlexLayout.Basis="60%">

                <Label Text="Title" FontSize="Large"  FontAttributes="Bold"/>

                <ScrollView>
                    <Label Text="Lorem Ipsum is simply dummy text of the printing and typesetting industry.Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,when an unknown printer took a galley of type and scrambled it to make a type specimen book.It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."
                           FontSize="Medium" />
                </ScrollView>

            </StackLayout>

        </FlexLayout>

    </ContentPage.Content>
       

</ContentPage>
  • As in the above MainPage.xaml I set FlexLayout direction to ‘Column’ so children of this will appear vertically.
  • Here, There are two childrens of FlexLayout that are Image and StackLayout.

1 – Image : I set 40% of the screen to this image by using below code.

 FlexLayout.Basis=”40%”

Now, This will take 40% height of screen in portrait mode and 40% width of screen in landscape mode. 

2 – StackLayout : I set 60% of the screen to this stacklayout by using below code.

 FlexLayout.Basis=”60%”

Now, This will take 60% height of screen in portrait mode and 60% width of screen in landscape mode.

  • As you will see in MainPage.xaml I used VisualStateManager for managing the states.
  • I have created two states:
Portrait State – For this state I ‘m setting ‘Direction’ as ‘Column’ so children of this will appear vertically.
Landscape State – For this state I’m setting ‘Direction’ as ‘Row’ so children of this will appear horizontally.
  • Now, we need to override the OnSizeAllocated() method in MainPage.xaml.cs to get the orientation.

1.2 – MainPage.xaml.cs

namespace XFHandleViewOnOrientationChangeSample
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        protected override void OnSizeAllocated(double width, double height)
        {
            base.OnSizeAllocated(width, height);

            if (width > height)
            {
                // Landscape
                VisualStateManager.GoToState(FlexLayout, "Landscape");
            }
            else
            {
                // Portrait
                VisualStateManager.GoToState(FlexLayout, "Portrait");
            }
        }
    }
}
  • In this override method based on orientation I’m just switching the states. That’s all 😀

2 – Result

That’s all for now!

You can check the full source code here.

Happy Coding! 😀

You may also like

Field Modifiers in MAUI

How to use Field Modifiers in MAUI

get device and app informations in maui

How to get Device and App Informations in MAUI

Post Views: 333
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 11+ 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 use CollectionView as a GridItemLayout in Xamarin Forms
  • How to change StatusBar color in MAUI
  • .Net MAUI – The Latest CarouselView with IndicatorView

Latest Comments

  1. knight online makro on Implementation Of Biometric Authentication in Xamarin FormsFebruary 12, 2026

    Your blog is always a highlight of my day

  2. dịch vụ backlink top.com vn on How to use CollectionView as a GridItemLayout in Xamarin FormsNovember 20, 2025

    Great post, you have pointed out some fantastic details , I besides think this s a very superb website.

  3. ryzen sunucu kirala on Implementation Of Biometric Authentication in Xamarin FormsNovember 4, 2025

    Thank you for sharing your personal experience and wisdom with us Your words are so encouraging and uplifting

  4. dofollow backlinks means on How to use FlexLayout in MAUIOctober 29, 2025

    Perfect piece of work you have done, this site is really cool with superb info .

  5. backlinks que es on How to use ObservableObject in MAUI for Data BindingOctober 28, 2025

    I like this internet site because so much useful stuff on here : D.

Our Visitor

0 0 8 9 3 3
Users Today : 3
Users Yesterday : 3
Users Last 7 days : 23
Users Last 30 days : 97
Users This Month : 3
Total views : 15359
How to use FlexLayout in MAUI
Trending
How to use FlexLayout in MAUI

Category

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

Contact Me

  • LinkedIn
      © 2026 Learn Mobile Development