How to use setRequestedOrientation

222 Views Asked by At

I am using Visual Studio 2019, C#, for Android.

I am trying to set the orientation and lock it. When I follow the examples, I get the error message:

The name 'setRequestedOrientation' does not exist in the current context

This is a stripped-down beginning of my MainActivity.cs file:

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Content;
using System;
using System.Text;
using SQLite;
using Android.Support.V4.Content;
using Android;
using Android.Support.V4.App;
using Android.Content.PM;
using System.IO;
using Android.Bluetooth;
using System.Collections;
using Java.Util;
using System.Collections.Generic;

namespace Fubar
{
    [Activity(Label = "Fubar", MainLauncher = true, Icon = "@drawable/icon", ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]
    public partial class MainActivity : Android.Support.V7.App.AppCompatActivity
    {
        public static Android.Support.V4.Widget.DrawerLayout drawerLayout;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            Vars.Is_Land = false;
            base.OnCreate(savedInstanceState);
            Vars.thisApp = this;

            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    // .
    // .
    // .

What do I need to add to make this work? I assume that I have to add another "using" line.

This app has been around for a long time, so it currently supports Android 4.4.x (KitKat) and above. Do I need to stop supporting some of these early versions to get this working?

2

There are 2 best solutions below

4
AudioBubble On BEST ANSWER

The problem with your code is that you are mixing Android Java with Android Xamarin. Given that Android Xamarin is a binding of the earlier then you need to know that any method in Android Java needs to follow the C# method naming convention when used from the Android Xamarin context. In your case you're using an Android Java method.

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

which will not compile because that method already broke the Xamarin Android C# naming convention. The method does exist for Xamarin Android as a property of the Activity class object and you can use it like shown in the code snippet below.

RequestedOrientation = ScreenOrientation.Landscape;.

0
user2153142 On

Example:

In a fragment's OnCreate

if (Activity.RequestedOrientation != ScreenOrientation.SensorPortrait)
                Activity.RequestedOrientation = ScreenOrientation.SensorPortrait;

And in the fragment's OnDestroy

Activity.RequestedOrientation = ScreenOrientation.Unspecified;

I've got no idea if that is supported on KitKat, probably is, but KitKat is not supported any more. I think API 21 is the lowest at the moment, soon to be API 24.

In an app say supporting Android 13, either Xamarin.Android or net7.0-android your main activity would be like the following

using AndroidX.AppCompat.App;

public class MainActivity : AppCompatActivity

Therefore you would need to get rid of all the old support lib stuff e.g. Android.Support.V7.App.AppCompatActivity and start using the AndroidX replacements.

You can find many samples re modern Android development with Xamarin on my GitHub https://github.com/gmck