question

Jayaram-4146 avatar image
0 Votes"
Jayaram-4146 asked ColeXia-MSFT answered

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

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.

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ColeXia-MSFT avatar image
0 Votes"
ColeXia-MSFT answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.