How to change screen orientation in Maui?

chrissolutions 1 Reputation point

In Xamarin there was a class called DeviceInfo with a settable CurrentOrientation property.
This class appears to not exist for Maui. I can't figure out how you can change the screen orientation and I can't find an example that can show how's it's accomplished. Can anyone here provide me with some guidance. Thanks in advance.

A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
888 questions
No comments
{count} votes

2 answers

Sort by: Most helpful
  1. muneer abu safiah 11 Reputation points

    for android, just add

    ScreenOrientation= ScreenOrientation.Landscape  

    in Platforms\Android\mainActivity.cs, before public class MainActivity : MauiAppCompatActivity it will look like this:

    [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ScreenOrientation= ScreenOrientation.Landscape, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]  
    No comments

  2. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 41,411 Reputation points Microsoft Employee


    Maui doesn't provide a cross platform property to change the screen orientation, but Maui gives access to the underlying native API

    Please refer to this document about Invoke platform code

    Android we can change the Orientation by RequestedOrientation, set the ScreenOrientation directly.

       //Set ScreenOrientation to Landscape  
                       MainActivity.Instance.RequestedOrientation = ScreenOrientation.Landscape;  
       //Set ScreenOrientation to Portrait  
                       MainActivity.Instance.RequestedOrientation = ScreenOrientation.Portrait;  

    Note: MainActivity.Instance comes from MainActivity.cs, I define a public static property

    iOS: we can use UIDevice.CurrentDevice.SetValueForKey and set it for different Orientation.

       //Set ScreenOrientation to Landscape  
                       UIDevice.CurrentDevice.SetValueForKey(NSNumber.FromNInt((int)(UIInterfaceOrientation.LandscapeLeft)), new NSString("orientation"));  
       //Set ScreenOrientation to Portrait  
                       UIDevice.CurrentDevice.SetValueForKey(NSNumber.FromNInt((int)(UIInterfaceOrientation.Portrait)), new NSString("orientation"));  

    Best Regards,

    Leon Lu

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.