How to change screen orientation in Maui?

chrissolutions 26 Reputation points
2022-06-07T00:25:30.037+00:00

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.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,861 questions
0 comments No comments
{count} vote

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,491 Reputation points Microsoft Vendor
    2022-06-07T09:28:38.207+00:00

    Hello,​

    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.


2 additional answers

Sort by: Most helpful
  1. muneer abu safiah 11 Reputation points
    2022-10-25T07:52:14.617+00:00

    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)]  
    
    2 people found this answer helpful.
    0 comments No comments

  2. Marian Grzesik 5 Reputation points
    2023-02-11T16:12:29.9933333+00:00

    Hi, I tried the iOS solution, unfortunately it doesn't work for iOS / .NET 7

    1 person found this answer helpful.