0

It is said here and here that using ScreenOrientation = ScreenOrientation.Portrait can force-lock the app's orientation:

[Activity(
    Label = "SomeApplication",
    MainLauncher = true,
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
    ScreenOrientation = ScreenOrientation.Portrait
)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;
        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());
    }
}

Also tried editing AndroidManifest.xml like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.package.SomeApplication">
    <uses-sdk android:minSdkVersion="15" />
    <application android:label="SomeApplication.Android">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait">
        </activity>
    </application>
</manifest>

and this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.package.SomeApplication">
    <uses-sdk android:minSdkVersion="15" />
    <application android:label="SomeApplication.Android">
    </application>
    <activity
          android:name=".MainActivity"
          android:screenOrientation="portrait">
    </activity>
</manifest>

But every time I run the app, I can still rotate it in landscape mode. My phone also has Auto-Rotate enabled, but I don't think that is an issue.

What else did I miss?

Carl Binalla
  • 5,393
  • 5
  • 27
  • 46

2 Answers2

0

remove this code from MainActivity and add this only in AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:roundIcon="@mipmap/ic_launcher"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

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

</application>
Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71
0

Another way is to set the orientation programmatically: Just call

this.RequestedOrientation = ScreenOrientation.Portrait; in OnResume()

to set it to portrait mode

Because xamarin suggested the way you implemented first and failed. https://developer.xamarin.com/guides/xamarin-forms/user-interface/layouts/device-orientation/