{"id":178,"date":"2022-11-20T19:45:53","date_gmt":"2022-11-20T19:45:53","guid":{"rendered":"https:\/\/learnmobiledevelopment.com\/?p=178"},"modified":"2023-01-18T17:17:31","modified_gmt":"2023-01-18T17:17:31","slug":"rotate-views-on-orientation-change","status":"publish","type":"post","link":"https:\/\/learnmobiledevelopment.com\/index.php\/2022\/11\/20\/rotate-views-on-orientation-change\/","title":{"rendered":"How to rotate views on orientation change in Xamarin Forms"},"content":{"rendered":"\n<p>It is very important to handle the pages on both orientation <strong>Landscape<\/strong> and <strong>Portrait<\/strong> to improve the user experience. So we need to adjust the existing view (without recreating it) in such a way that looks good in <strong>Landscape<\/strong> and <strong>Portrait<\/strong> mode. Xamarin forms handle orientation changes by default. So In this article, I\u2019m going to show you <strong>how to rotate views on orientation change without recreating layout<\/strong>.<\/p>\n\n\n\n<p style=\"font-size:25px\"><strong>Let\u2019s Start<\/strong><\/p>\n\n\n\n<p>To create such a type of page in xamarin forms we will use <strong>FlexLayout <\/strong>and<strong> VisualStateManager<\/strong>.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>What is FlexLayout?<\/strong><\/p>\n\n\n\n<p><strong>FlexLayout<\/strong> in xamarin forms is a layout which is similar to <strong>StackLayout<\/strong>. <strong>FlexLayout<\/strong> 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 <strong>FlexLayout<\/strong> <a href=\"https:\/\/learn.microsoft.com\/en-us\/xamarin\/xamarin-forms\/user-interface\/layouts\/flex-layout\" target=\"_blank\" rel=\"noopener\" title=\"\">here<\/a>.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>What is VisualStateManager<\/strong>?<\/p>\n\n\n\n<p><strong><strong>VisualStateManager<\/strong> <\/strong>in xamarin forms is a way to make visual changes to UI from code behind. It introduces the concept of <strong>states<\/strong>. Let&#8217;s take an example of a button to explain states, Button has <strong>Normal<\/strong> state, <strong>Disabled<\/strong> state and <strong>Pressed<\/strong> state. We can also create custom states. You can learn more about <strong><strong>VisualStateManager<\/strong><\/strong> <a href=\"https:\/\/learn.microsoft.com\/en-us\/xamarin\/xamarin-forms\/user-interface\/visual-state-manager\" target=\"_blank\" rel=\"noopener\" title=\"\">here<\/a>.<\/p>\n\n\n\n<p>In this sample demo I will create two custom states that are <strong>Portrait<\/strong> state and <strong>Landscape<\/strong> state.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>1 &#8211; Setting up the UI<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>1.1 &#8211; Create a MainPage.xaml<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"419\" src=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/11\/ChangeViewOnOrientationChanges-1024x419.png\" alt=\"Rotate views on orientation change\" class=\"wp-image-222\" srcset=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/11\/ChangeViewOnOrientationChanges-1024x419.png 1024w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/11\/ChangeViewOnOrientationChanges-300x123.png 300w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/11\/ChangeViewOnOrientationChanges-768x314.png 768w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/11\/ChangeViewOnOrientationChanges-850x348.png 850w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/11\/ChangeViewOnOrientationChanges.png 1222w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" encoding=\"utf-8\" ?>\n&lt;ContentPage xmlns=\"http:\/\/xamarin.com\/schemas\/2014\/forms\"\n             xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2009\/xaml\"\n             x:Class=\"XFHandleViewOnOrientationChangeSample.MainPage\">\n\n     &lt;ContentPage.Resources>\n        &lt;ResourceDictionary>\n            &lt;Style TargetType=\"FlexLayout\">\n                &lt;Setter Property=\"VisualStateManager.VisualStateGroups\">\n                    &lt;VisualStateGroupList>\n                        &lt;VisualStateGroup x:Name=\"OrientationState\">\n\n                            &lt;!--  Portrait  -->\n                            &lt;VisualState x:Name=\"Portrait\">\n                                &lt;VisualState.Setters>\n                                    &lt;Setter Property=\"Direction\" Value=\"Column\"\/>\n                                &lt;\/VisualState.Setters>\n                            &lt;\/VisualState>\n\n                            &lt;!--  Landscape  -->\n                            &lt;VisualState x:Name=\"Landscape\">\n                                &lt;VisualState.Setters>\n                                    &lt;Setter Property=\"Direction\" Value=\"Row\" \/>\n                                &lt;\/VisualState.Setters>\n                            &lt;\/VisualState>\n\n                        &lt;\/VisualStateGroup>\n                    &lt;\/VisualStateGroupList>\n                &lt;\/Setter>\n            &lt;\/Style>\n        &lt;\/ResourceDictionary>\n    &lt;\/ContentPage.Resources>\n\n    &lt;ContentPage.Content>\n\n         &lt;FlexLayout\n            x:Name=\"FlexLayout\"\n            Direction=\"Column\"\n            Wrap=\"NoWrap\">\n\n            &lt;Image\n                x:Name=\"Image\"\n                Aspect=\"AspectFill\"\n                FlexLayout.Basis=\"40%\"\n                Source=\"img.jpg\"\/>\n\n\n            &lt;StackLayout Margin=\"10\" FlexLayout.Basis=\"60%\">\n\n                &lt;Label Text=\"Title\" FontSize=\"Large\"  FontAttributes=\"Bold\"\/>\n\n                &lt;ScrollView>\n                    &lt;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.\"\n                           FontSize=\"Medium\" \/>\n                &lt;\/ScrollView>\n\n            &lt;\/StackLayout>\n\n        &lt;\/FlexLayout>\n\n    &lt;\/ContentPage.Content>\n       \n\n&lt;\/ContentPage><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>As in the above <strong>MainPage.xaml<\/strong> I set FlexLayout direction to <strong>\u2018Column\u2019<\/strong> so children of this will appear vertically.<\/li>\n\n\n\n<li>Here, There are two childrens of <strong>FlexLayout<\/strong> that are <strong>Image<\/strong> and <strong>StackLayout.<\/strong><\/li>\n<\/ul>\n\n\n\n<p>           <strong>1 &#8211; Image<\/strong> : I set 40% of the screen to this image by using below code.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>&nbsp;<strong>FlexLayout.Basis=&#8221;40%&#8221;<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Now, This will take 40% height of screen in <strong>portrait<\/strong> mode and 40% width of screen in <strong>landscape<\/strong> mode.&nbsp;<\/p>\n\n\n\n<p>          <strong>2 &#8211; StackLayout<\/strong> : I set 60% of the screen to this stacklayout by using below code.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>&nbsp;FlexLayout.Basis=&#8221;60%&#8221;<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Now, This will take 60% height of screen in <strong>portrait<\/strong> mode and 60% width of screen in <strong>landscape<\/strong> mode.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>As you will see in <strong>MainPage.xaml<\/strong> I used <strong>VisualStateManager<\/strong> for managing the states.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>I have created two states:<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Portrait State<\/strong> &#8211; For this state I &#8216;m setting <strong>\u2018Direction\u2019<\/strong> as <strong>\u2018Column\u2019 <\/strong>so children of this will appear vertically.<br><strong>Landscape State<\/strong> &#8211; For this state I&#8217;m setting <strong>\u2018Direction\u2019<\/strong> as \u2018<strong>Row\u2019<\/strong> so children of this will appear horizontally.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Now, we need to override the <strong>OnSizeAllocated()<\/strong> method in <strong>MainPage.xaml.cs <\/strong>to get the orientation.<\/li>\n<\/ul>\n\n\n\n<p class=\"has-medium-font-size\"><strong>1.2 &#8211; MainPage.xaml.cs <\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">namespace XFHandleViewOnOrientationChangeSample\n{\n    public partial class MainPage : ContentPage\n    {\n        public MainPage()\n        {\n            InitializeComponent();\n        }\n\n        protected override void OnSizeAllocated(double width, double height)\n        {\n            base.OnSizeAllocated(width, height);\n\n            if (width > height)\n            {\n                \/\/ Landscape\n                VisualStateManager.GoToState(FlexLayout, \"Landscape\");\n            }\n            else\n            {\n                \/\/ Portrait\n                VisualStateManager.GoToState(FlexLayout, \"Portrait\");\n            }\n        }\n    }\n}\n<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In this override method based on orientation I&#8217;m just switching the states. That\u2019s all \ud83d\ude00<\/li>\n<\/ul>\n\n\n\n<p class=\"has-medium-font-size\"><strong>2 &#8211; Result<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"2796\" style=\"aspect-ratio: 1290 \/ 2796;\" width=\"1290\" controls muted src=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/RotateViewOnOrientationChange.mp4\"><\/video><\/figure>\n\n\n\n<p>That\u2019s all for now!<\/p>\n\n\n\n<p>You can check the full source code <strong><a href=\"https:\/\/github.com\/Alam-Ashraf\/XFHandleViewOnOrientationChangeSample\" target=\"_blank\" rel=\"noopener\" title=\"\">here<\/a>.<\/strong><\/p>\n\n\n\n<p>Happy Coding!<strong> \ud83d\ude00<\/strong><\/p>\n\n\n\n<div style=\"height:35px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-medium-font-size\"><strong>You may also like<\/strong><\/p>\n\n\n\n<div class=\"wp-block-group is-content-justification-space-between is-nowrap is-layout-flex wp-container-core-group-is-layout-0dfbf163 wp-block-group-is-layout-flex\">\n<div class=\"wp-block-group is-vertical is-content-justification-center is-layout-flex wp-container-core-group-is-layout-4b2eccd6 wp-block-group-is-layout-flex\"><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/learnmobiledevelopment.com\/index.php\/2023\/01\/08\/field-modifiers-in-maui\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/FieldModifier.jpg\" alt=\"Field Modifiers in MAUI\" class=\"wp-image-666\" width=\"372\" height=\"221\" srcset=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/FieldModifier.jpg 496w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/FieldModifier-300x178.jpg 300w\" sizes=\"auto, (max-width: 372px) 100vw, 372px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p class=\"has-text-align-center\">How to use Field Modifiers in MAUI<\/p>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-center is-layout-flex wp-container-core-group-is-layout-4b2eccd6 wp-block-group-is-layout-flex\">\n<div class=\"wp-block-group is-vertical is-content-justification-center is-layout-flex wp-container-core-group-is-layout-4b2eccd6 wp-block-group-is-layout-flex\"><div class=\"wp-block-image is-style-default\">\n<figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/learnmobiledevelopment.com\/index.php\/2023\/01\/04\/get-device-and-app-informations-in-maui\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/Device-And-App-Info-1024x684.png\" alt=\"get device and app informations in maui\" class=\"wp-image-653\" width=\"512\" height=\"342\" srcset=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/Device-And-App-Info-1024x684.png 1024w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/Device-And-App-Info-300x200.png 300w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/Device-And-App-Info-768x513.png 768w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/Device-And-App-Info-850x568.png 850w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/Device-And-App-Info.png 1296w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p class=\"has-text-align-center\">How to get Device and App Informations in MAUI<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>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\u2019m going&#8230;<\/p>\n","protected":false},"author":1,"featured_media":181,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[17,20,19,8],"class_list":["post-178","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-xamarin","tag-handling-orientation-changes-in-xamarin","tag-handling-rotation-in-xamarin","tag-how-to-rotate-views-in-place-when-orientation-changes-2","tag-rotate-views-on-orientation-change"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/posts\/178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/comments?post=178"}],"version-history":[{"count":48,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/posts\/178\/revisions"}],"predecessor-version":[{"id":767,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/posts\/178\/revisions\/767"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/media\/181"}],"wp:attachment":[{"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/media?parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/categories?post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/tags?post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}