How to rotate screen manually as well automatically in Xamarin.forms?

Jayaram 1 Reputation point
2021-05-05T21:40:26.507+00:00

I have a requirement to rotate screen manually as well as automatically.

Manual: Rotate the screen from Landscape to Portrait. Then I should show the Portrait view of the page even though I have not tilted my phone(still in landscape). Both portrait and landscape have different views with buttons at the bottom to change orientation. By tapping the button I should change the orientation even though I stay in Landscape I should show a portrait view.

Automatic: When I rotate the phone also it should change view automatically.

Achieving both separately is possible. Even I can get hundreds of answers already on google. But when I want to achieve both cases for the same page is a hurddle.

To make it clear and simplified. Find below

  1. I want to rotate only one page in my app automatically and as well as manually. (Achieved)
  2. Both portrait and landscape views are different. (Achieved)
  3. I should not lock the particular page at any cause but I also when I change orientation manually I should able to do that. Even though I am not rotating/tilt the phone. (Todo)

I want to satisfy both cases. How to achieve that in Android and iOS (Xamarin.Forms)?

Anyone faces this kind of use case and if you got a solution. Help me out.

Thanks in advance.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,297 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2021-05-07T03:22:46.47+00:00

    Hello,

    Welcome to Microsoft Q&A!

    By tapping the button I should change the orientation even though I stay in Landscape I should show a portrait view.

    We can use messaging center to force Landscape or Portrait on each platform.

    Forms

       private void Button_Clicked(object sender, EventArgs e)  
               {  
                   MessagingCenter.Send<object,string>(this, "ChangeOrientation", "Landscape");  
               }  
    

    Android

       protected override void OnCreate(Bundle savedInstanceState)  
               {  
                   TabLayoutResource = Resource.Layout.Tabbar;  
                   ToolbarResource = Resource.Layout.Toolbar;  
         
                   base.OnCreate(savedInstanceState);  
         
         
                   MessagingCenter.Subscribe<object,string>(this, "ChangeOrientation", (sender,args) =>  
                   {  
                       if(args == "Landscape")  
                       {  
                           RequestedOrientation = ScreenOrientation.Unspecified;  
                       }  
                       else if (args == "Portrait")  
                       {  
                           RequestedOrientation = ScreenOrientation.Portrait;  
                       }              
                   });  
         
                   Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
                   global::Xamarin.Forms.Forms.Init(this, savedInstanceState);  
                   LoadApplication(new App());  
         
       }  
    

    iOS

       public override bool FinishedLaunching(UIApplication app, NSDictionary options)  
               {  
                   global::Xamarin.Forms.Forms.Init();  
         
                   MessagingCenter.Subscribe<object, string>(this, "ChangeOrientation", (sender, args) =>  
                   {  
                       if (args == "Landscape")  
                       {  
                           UIDevice.CurrentDevice.SetValueForKey(NSNumber.FromNInt((int)(UIInterfaceOrientation.LandscapeLeft)), new NSString("orientation"));  
                       }  
                       else if (args == "Portrait")  
                       {  
                           UIDevice.CurrentDevice.SetValueForKey(NSNumber.FromNInt((int)(UIInterfaceOrientation.Portrait)), new NSString("orientation"));  
                       }  
                   });  
         
                   LoadApplication(new App());  
                   return base.FinishedLaunching(app, options);  
               }  
    

    Best Regards,
    Cole Xia


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments