In my activity, I have the following code.

[Activity(Label = "SampleActivity", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize | Android.Content.PM.ConfigChanges.KeyboardHidden)]

In OnCreate() ->

 if (Build.VERSION.SdkInt >= Build.VERSION_CODES.Gingerbread) {
                RequestedOrientation = ScreenOrientation.SensorLandscape;
            }
            else {
                RequestedOrientation = ScreenOrientation.Landscape;
            }

Still the screen flips in landscape mode itself if the Auto rotate is ON.Would like to know the reason why this happens?.Is there any option to avoid flipping?

Need help.Thanks in advance.

2

There are 2 best solutions below

2
On

Try writing this in Menifest file:

<activity
            android:name="SampleActivity"
            android:screenOrientation="landscape" // or "portrait" if you want
            android:configChanges="keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name" />

For Xamarin change your code according to your need as: (ScreenOrientation.Portrait / ScreenOrientation.Landscape)

[Activity(Label = "SampleActivity", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize | Android.Content.PM.ConfigChanges.KeyboardHidden),ScreenOrientation = ScreenOrientation.Portrait)]
2
On

In order to lock your app in portrait mode, You have to add following line in your manifest file where you are defining your activity in the manifest file.

 <activity android:name=".yourActivity" android:screenOrientation="portrait"/>

If you want to lock in landscape mode replace portrait with landscape.