0

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?

1
  • You can change the setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); to this.RequestedOrientation = ScreenOrientation.Landscape;. Commented Apr 13, 2023 at 6:30

2 Answers 2

2

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;.

Sign up to request clarification or add additional context in comments.

4 Comments

The documentation can be found from this Microsoft link google.com/url?sa=t&source=web&rct=j&url=https://…
Binding of the earlier what? A word seems to be missing.
@PeterMortensen, are you doing this because I visited your posts and left some comments?
@PeterMortensen, Android Xamarin is a C# binding of the earlier which is Android Java. What's your consensus?
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.