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: 264
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 use ObservableObject in MAUI for Data Binding
  • Implementation Of Biometric Authentication in Xamarin Forms
  • How to use CollectionView as a GridItemLayout in Xamarin Forms

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 8 6 2
Users Today : 9
Users Yesterday : 4
Users Last 7 days : 33
Users Last 30 days : 151
Users This Month : 83
Total views : 14085
How to apply color to Images and Icons in MAUI
Trending
How to apply color to Images and Icons in MAUI

Category

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

Contact Me

  • LinkedIn
      © 2025 Learn Mobile Development