31

Is it possible to prevent the orientation of an Android application from changing ? I have an application that previews the phone camera and I would like the orientation not to change.

I have tried adding this to the manifest but it did not make a difference

<application android:label="TextureViewCameraStream" android:hardwareAccelerated="true">
    </application>
    <activity
       android:screenOrientation="portrait"
       android:configChanges="orientation|keyboardHidden">
    </activity>

I am using Xamarin for Android and C#

Thank you

G-Man
  • 7,232
  • 18
  • 72
  • 100

2 Answers2

54

Set it in the Activity flag like this:

[Activity (Label = "YourActivityname", MainLauncher = true, ScreenOrientation = ScreenOrientation.Portrait)] 
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • 3
    Anybody else looking for an answer [for xamarin iOS as well](https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/device-orientation?tabs=macos), it can be set in `Info.plist`. – lucidbrot Mar 27 '19 at 08:49
4

If you have an Activity, and you want to keep your activity in the same orientation when it is opened, then use this label:

[Activity(Label = "MyApp", ScreenOrientation = Android.Content.PM.ScreenOrientation.Locked)]
Led Machine
  • 7,122
  • 3
  • 47
  • 49