Hey,
Today I tried to lock the Screen-Orientation of the App I'm currently developing. I wanted to lock the Orientation on Phones to Portrait and on Tablets it have to be Landscape.
Normally I would create Layouts
for Landscape
and Portrait
mode for both Idioms, but in this case that is not the way to go.
Think of Angry-Birds in Portrait Mode on a Phone. Yeah that would be a crappy experience
So I worked on that and was finally able to do it.
Now here's what I've done:
On Android you have to set those flags in MainActivity.cs
:
[Activity(Label = "YourAppName", Icon = "@drawable/Icon", Theme = "@style/StandardTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
And in the OnCreate
you write those lines before base.OnCreate(bundle);
switch(Device.Idiom)
{
case TargetIdiom.Phone:
RequestedOrientation = ScreenOrientation.Portrait;
break;
case TargetIdiom.Tablet:
RequestedOrientation = ScreenOrientation.Landscape;
break;
}
So I tested this on my Tablet and hell yeah it worked. I couldn't change the Orientation anymore.
But then I tested it on my Phone and hell no it didn't worked
I searched for a few hours for a solution why it won't work and tested a lot of stuff that I found here in the forums or on SO.
And then I stumbled upon this SO Thread: Stack Overflow.
Here was the problem. It was my Phone! I have an Samsung Galaxy Note 2.
So I searched on the internet what is going on and found out that different devices ignoring the RequestedOrientation
flag
Then I remember that in MainActivity.cs
you can override this:
public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)
The Configuration in here has a Orientation property which will give you access to the current Orientation
Now I hooked it up and got this solution:
public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)
{
base.OnConfigurationChanged(newConfig);
switch(newConfig.Orientation)
{
case Orientation.Landscape:
switch(Device.Idiom)
{
case TargetIdiom.Phone:
LockRotation(Orientation.Portrait);
break;
case TargetIdiom.Tablet:
LockRotation(Orientation.Landscape);
break;
}
break;
case Orientation.Portrait:
switch(Device.Idiom)
{
case TargetIdiom.Phone:
LockRotation(Orientation.Portrait);
break;
case TargetIdiom.Tablet:
LockRotation(Orientation.Landscape);
break;
}
break;
}
}
private void LockRotation(Orientation orientation)
{
switch(orientation)
{
case Orientation.Portrait:
RequestedOrientation = ScreenOrientation.Portrait;
break;
case Orientation.Landscape:
RequestedOrientation = ScreenOrientation.Landscape;
break;
}
}
Now everything worked as it should. Now even on my Galaxy Note 2 it didn't changed the Orientation anymore.
For iOS it was really simple compared to Android Just put this in your AppDelegate.cs
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, UIWindow forWindow)
{
switch(Device.Idiom)
{
case TargetIdiom.Phone:
return UIInterfaceOrientationMask.Portrait;
case TargetIdiom.Tablet:
return UIInterfaceOrientationMask.Landscape;
default:
return UIInterfaceOrientationMask.Portrait;
}
}
I hope this helps someone who is struggling with the same thing