Xamarin Android, Xamarin Forms orientation change - xamarin.android

Xamarin Android app that use the Xamarin Forms UI framework.
I detect the screen orientation and want to only allow portrait and landscape for a specific Page but portrait only for all other pages.
I tried to call RequestOrientation but that force the orientation to stay the same and doesn't fire my orientation change notification again.

Have a static property in your App.Xaml.cs
public static bool IsPortraitOnly { get; set; }
Set it to false in all pages and to true in the page you want it to be Portrait only.
In your MainActivity in android project, override the method OnConfigurationChanged :
public override void OnConfigurationChanged(Configuration newConfig)
{
//using to prevent the OnCreate from firing when rotating or screen size is changing
if (App.IsPortraitOnly)
{
RequestedOrientation = ScreenOrientation.Portrait;
newConfig.Orientation = Orientation.Portrait;
}
base.OnConfigurationChanged(newConfig);
}
The following are the available orientation values in android :
public enum Orientation
{
Landscape = 2,
Portrait = 1,
Square = 3,
Undefined = 0
}

To determine whether you’re in portrait or landscape mode is pretty easy:
static bool IsPortrait(Page p) { return p.Width < p.Height; }
you can use sizeChanged event.The SizeChanged event seems to get called exactly once as the user goes from portrait to landscape mode
SizeChanged += (sender, e) => Content = IsPortrait(this) ? portraitView : landscapeView;

RequestedOrientation = ScreenOrientation.Portrait;
If you write code on the code side, portrait mode is maintained.

Related

Xamarin (Android): Detect if the device has a display notch or not

I'm trying to detect if the device my app is running on has a notch or not.
This has what I need but I'm not been able to find the Xamarin equivalent for getDisplayCutout() in the WindowInset class.
There are some differences between native Android and Xamarin.Android .
In your case , the method getDisplayCutout() in Xamarin.Android is readonly property called DisplayCutout .
public DisplayCutout DisplayCutout { get; }
You could access it like
Android.Views.WindowInsets window = new WindowInsets(cpoySrc);
var Cutout = window.DisplayCutout;
cpoySrc is a source WindowInsets. Check https://learn.microsoft.com/en-us/dotnet/api/android.views.windowinsets.-ctor?view=xamarin-android-sdk-9
I managed to 'solve' this issue by measuring the size of the status bar and comparing it against a known/safe threshold.
Won't claim this to be the best solution to this question but it holds up against the devices I've tested so far.
private const int __NOTCH_SIZE_THRESHHOLD = 40; //dp
/// <summary>
/// Device has a notched display (or not)
/// </summary>
public bool HasNotch
{
get
{
// The 'solution' is to measure the size of the status bar; on devices without a notch, it returns 24dp.. on devices with a notch, it should be > __NOTCH_SIZE_THRESHHOLD (tested on emulator / S10)
int id = MainActivity.Current.Resources.GetIdentifier("status_bar_height", "dimen", "android");
if (id > 0)
{
int height = MainActivity.Current.Resources.GetDimensionPixelSize(id);
if (pxToDp(height) > __NOTCH_SIZE_THRESHHOLD)
return true;
}
return false;
}
}
/// <summary>
/// Helper method to convert PX to DP
/// </summary>
private int pxToDp(int px)
{
return (int)(px / Android.App.Application.Context.Resources.DisplayMetrics.Density);
}

iPhone simulator (detect different device simulators)

With the upcoming release of the iPhone X, I want to be able to display a different UI layout for the iPhone X (due to round corners and bottom line, which kinda works as the home button replacement).
I am using the following nuget package to retrieve the model information:
https://github.com/dannycabrera/Get-iOS-Model
It works perfectly fine, but all the different simulators (iPhone 7, 8, X) only come up as Simulator.
Is there a way to differentiate between the different iPhone Simulators in code within my Xamarin mobile app?
Many thanks,
Nik
Since the simulator is a weird animal, the screen size is as good as any other test after the other tests for iOS version and the availability of FaceID on a physical device:
public bool iPhoneX()
{
var version = new Version(ObjCRuntime.Constants.Version);
if (version < new Version(11, 0))
return false;
if (ObjCRuntime.Runtime.Arch == ObjCRuntime.Arch.DEVICE)
{
using (var context = new LocalAuthentication.LAContext())
{
if (context.BiometryType == LABiometryType.TypeFaceId)
return true;
}
return false;
}
if (UIScreen.MainScreen.PreferredMode.Size.Height == 2436)
return true;
return false;
}
Or an optimized property for repeated (binding) calls:
static bool? iPhoneX;
public bool isPhoneX
{
get
{
if (iPhoneX == null)
{
if (new Version(ObjCRuntime.Constants.Version) < new Version(11, 0))
iPhoneX = false;
else
{
if (ObjCRuntime.Runtime.Arch == ObjCRuntime.Arch.DEVICE)
{
using (var context = new LocalAuthentication.LAContext())
{
iPhoneX = context.BiometryType == LABiometryType.TypeFaceId;
}
}
else
iPhoneX = UIScreen.MainScreen.PreferredMode.Size.Height == 2436;
}
}
return (bool)iPhoneX;
}
}
You should simply use the Safe Area Layout Guide which will automatically increase the top/bottom margin on the iPhone X.
As others have pointed out you should definitely be using the Safe Area Layout Guide.
If you really really have a need to detect the model then look for the SIMULATOR_MODEL_IDENTIFIER environment variable. iPhone X will return iPhone10,3.
let model = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"]

In Swift, how to get the device orientation correctly right after it's launched?

I use following code to see if the device is in landscape mode or not:
UIDevice.currentDevice().orientation.isLandscape.boolValue
It works BUT if I put my device in landscape mode before the app is launched, and after viewDidLoad, I call this line of code, it always returns false.
If I use this instead:
interfaceOrientation.isLandscape
it returns true, which is correct, but the compiler is showing a warning that interfaceOrientation was deprecated in iOS 8.0.
What is the correct way to get the device orientation right after the app is launched?
DeviceOrientation vs. ScreenSize vs StatusBar.isLandscape?
iOS 11, Swift 4 and Xcode 9.X
Regardless of using AutoLayout or not, there are several ways to get the right device orientation, and they could be used to detect rotation changes while using the app, as well as getting the right orientation at app launch or after resuming from background.
This solutions work fine in iOS 11 and Xcode 9.X
1. UIScreen.main.bounds.size:
If you only want to know if the app is in landscape or portrait mode, the best point to start is in viewDidLoad in the rootViewController at launch time and in viewWillTransition(toSize:) in the rootViewController if you want to detect rotation changes while the app is in background, and should resume the UI in the right orientation.
let size = UIScreen.main.bounds.size
if size.width < size.height {
print("Portrait: \(size.width) X \(size.height)")
} else {
print("Landscape: \(size.width) X \(size.height)")
}
This also happens early during the app/viewController life cycles.
2. NotificationCenter
If you need to get the actual device orientation (including faceDown, faceUp, etc). you want to add an observer as follows (even if you do it in the application:didFinishLaunchingWithOptions method in the AppDelegate, the first notifications will likely be triggered after the viewDidLoad is executed
device = UIDevice.current
device?.beginGeneratingDeviceOrientationNotifications()
notificationCenter = NotificationCenter.default
notificationCenter?.addObserver(self, selector: #selector(deviceOrientationChanged),
name: Notification.Name("UIDeviceOrientationDidChangeNotification"),
object: nil)
And add the selector as follows. I split it in 2 parts to be able to run inspectDeviceOrientation() in viewWillTransition
#objc func deviceOrientationChanged() {
print("Orientation changed")
inspectDeviceOrientation()
}
func inspectDeviceOrientation() {
let orientation = UIDevice.current.orientation
switch UIDevice.current.orientation {
case .portrait:
print("portrait")
case .landscapeLeft:
print("landscapeLeft")
case .landscapeRight:
print("landscapeRight")
case .portraitUpsideDown:
print("portraitUpsideDown")
case .faceUp:
print("faceUp")
case .faceDown:
print("faceDown")
default: // .unknown
print("unknown")
}
if orientation.isPortrait { print("isPortrait") }
if orientation.isLandscape { print("isLandscape") }
if orientation.isFlat { print("isFlat") }
}
Note that the UIDeviceOrientationDidChangeNotification may be posted several times during launch, and in some cases it may be .unknown. What I have seen is that the first correct orientation notification is received after the viewDidLoad and viewWillAppear methods, and right before viewDidAppear, or even applicationDidBecomeActive
The orientation object will give you all 7 possible scenarios(from the enum UIDeviceOrientation definition):
public enum UIDeviceOrientation : Int {
case unknown
case portrait // Device oriented vertically, home button on the bottom
case portraitUpsideDown // Device oriented vertically, home button on the top
case landscapeLeft // Device oriented horizontally, home button on the right
case landscapeRight // Device oriented horizontally, home button on the left
case faceUp // Device oriented flat, face up
case faceDown // Device oriented flat, face down
}
Interestingly, the isPortrait read-only Bool variable is defined in an extension to UIDeviceOrientation as follows:
extension UIDeviceOrientation {
public var isLandscape: Bool { get }
public var isPortrait: Bool { get }
public var isFlat: Bool { get }
public var isValidInterfaceOrientation: Bool { get }
}
3. StatusBarOrientation
UIApplication.shared.statusBarOrientation.isLandscape
This also works fine to determine if orientation is portrait or landscape orientation and gives the same results as point 1. You can evaluate it in viewDidLoad (for App launch) and in viewWillTransition(toSize:) if coming from Background. But it won't give you the details of top/bottom, left/right, up/down you get with the notifications (Point 2)
This worked for me:
if UIScreen.main.bounds.width > UIScreen.main.bounds.height{
print("Portraitmode!")
}
It works on all devices based on the display dimensions:
https://developer.apple.com/library/archive/documentation/DeviceInformation/Reference/iOSDeviceCompatibility/Displays/Displays.html
I have tested many times about orientation, so I have summed up some experience.
In all iPhone devices, except iPhone6(s) plus, the only interface orientation is .portrait. If App is launched in landscape mode, there must be a change of orientation. One will receive the UIDeviceOrientationDidChangeNotification. It's an appropriate time to get the orientation.
Regarding the launching when in landscape with iPhone6, the orientation after the launch will change once:
The launching when in landscape with iPhone6 plus, after launch the orientation never changed:
Two different screenshot with the same app,
So before the app does change orientation, the orientation is still like in the home page.
In viewDidLoad, the orientation has not changed yet, the log will be the wrong direction.
The isValidInterfaceOrientation should be detected before checking the orientation isLandscape. Don't process the flat message with isValidInterfaceOrientation == false (when it has any value of isLandscape).
I had a hazzel with this until I read the topic more carefully. With consideration of the isValidInterfaceOrientation it works fine.
#objc func rotated() {
if (UIDevice.current.orientation.isValidInterfaceOrientation) {
if (UIDevice.current.orientation.isLandscape) {
if(!bLandscape) {
bLandscape = true
setupTabBar() // Repaint the app
}
} else { // Portait
if(bLandscape) {
bLandscape = false
setupTabBar() // Repaint the app
}
}
}
}
I had a problem to detect which orientation was before isFlat so I put this in my view controller
let orientation = UIDevice.current.orientation
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if orientation.isPortrait {
return .portrait
} else if orientation.isFlat{
if UIScreen.main.bounds.width < UIScreen.main.bounds.height{
return .portrait
} else {
return .landscape
}
} else {
return .landscape
}
}

Preventing the camera to rotate in iPad app using MvvmCross PictureChooser

I'm using Xamarin with MvvmCross to create an iPad application. In this application I use the PictureChooser plugin to take a picture with the camera. This all occurs in the way that can be seen in the related youtube video.
The code to accomplish this is fairly simple and can be found below. However when testing this on the actual device, the camera might be rotated.
private readonly IMvxPictureChooserTask _pictureChooserTask;
public CameraViewModel(IMvxPictureChooserTask pictureChooserTask)
{
_pictureChooserTask = pictureChooserTask;
}
private IMvxPictureChooserTask PictureChooserTask { get { return _pictureChooserTask; } }
private void TakePicture()
{
PictureChooserTask.TakePicture(400, 95,
async (stream) =>
{
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
var imageBytes = memoryStream.ToArray();
if (imageBytes == null)
return;
filePath = ProcessImage(imageBytes, FileName);
}
},
() =>
{
/* no action - we don't do cancellation */
}
);
}
This will lead to unwanted behavior. The camera should remain steady and be prevented in rotating within the App. I have been trying some stuff out, like preventing the app from rotating in the override bool ShouldAutorotate method while in camera mode, but unfortunately without any results.
Is there any setting that I forgot to set on the PictureChooser, or is the override method the item where I should perform some magic?
Thanks in advance.
Answer to this question has been raised in the comments of the question by user3455363, many thanks for this! Eventually it seemed to be a bug in iOS 8. The iOS 8.1 upgrade fixed this issue in my App!

How to fix alignment for vertical and portrait screen in BlackBerry?

I want to do application which will not change UI even if screen is changed to portrait or landscape direction. How to do that?
I have made a static method in one of my library classes:
public static void disableOrientationChange()
{
// force app to use only portrait mode
int directions = Display.DIRECTION_NORTH;
UiEngineInstance engineInstance = Ui.getUiEngineInstance();
if (engineInstance != null)
{
engineInstance.setAcceptableDirections(directions);
}
}
The trick is that this code only works for screens created after running it. So you must run the code BEFORE you show your first screen.
I call this method from my Application.main(String args) method, just before the call to enterEventDispatcher().
public static void main(String[] args)
{
MyApp app = new MyApp();
/*
* BlackBerry OS 5.0.0
* Disable screen orientation changes
*/
ScreenUtils.disableOrientationChange();
// enters the event processing loop thread if required
if (!app.isHandlingEvents())
{
app.enterEventDispatcher();
}
}
you can use this code to set the orientation to portrait. After that even if the device is held in landscape, the orientation wont change.
Ui.getUiEngineInstance().setAcceptableDirections(Display.DIRECTION_PORTRAIT);

Resources