How to Get the Current Screen Orientation

How can I get the current screen orientation?

Activity.getResources().getConfiguration().orientation

How do I get the CURRENT orientation (ActivityInfo.SCREEN_ORIENTATION_*) of an Android device?

I ended up using the following solution:

private int getScreenOrientation() {
int rotation = getWindowManager().getDefaultDisplay().getRotation();
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
int orientation;
// if the device's natural orientation is portrait:
if ((rotation == Surface.ROTATION_0
|| rotation == Surface.ROTATION_180) && height > width ||
(rotation == Surface.ROTATION_90
|| rotation == Surface.ROTATION_270) && width > height) {
switch(rotation) {
case Surface.ROTATION_0:
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
break;
case Surface.ROTATION_90:
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
break;
case Surface.ROTATION_180:
orientation =
ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
break;
case Surface.ROTATION_270:
orientation =
ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
break;
default:
Log.e(TAG, "Unknown screen orientation. Defaulting to " +
"portrait.");
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
break;
}
}
// if the device's natural orientation is landscape or if the device
// is square:
else {
switch(rotation) {
case Surface.ROTATION_0:
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
break;
case Surface.ROTATION_90:
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
break;
case Surface.ROTATION_180:
orientation =
ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
break;
case Surface.ROTATION_270:
orientation =
ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
break;
default:
Log.e(TAG, "Unknown screen orientation. Defaulting to " +
"landscape.");
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
break;
}
}

return orientation;
}

NOTE: Some users (Geltrude and holtaf in the comments below) pointed out that this solution will not work on all devices as the direction of rotation from the natural orientation is not standardized.

How to get current orientation of device programmatically in iOS 6?

You can try this, may help you out:

How to change the device orientation programmatically in iOS 6

OR

Objective-c:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]

Swift:

if UIDevice.current.orientation.isLandscape {
// Landscape mode
} else {
// Portrait mode
}

How to get current screen orientation in iOS Safari?

If you want to do some CSS, you can use media queries.

https://developer.mozilla.org/en-US/docs/Web/CSS/@media/orientation

Basically you can use @media (orientation: landscape) {}

If you want it in JS, for other purposes, you can use:

let orientation = (screen.orientation || {}).type || screen.mozOrientation || screen.msOrientation;

if (orientation === "landscape-primary") {
console.log("That looks good.");
} else if (orientation === "landscape-secondary") {
console.log("Mmmh... the screen is upside down!");
} else if (orientation === "portrait-secondary" || orientation === "portrait-primary") {
console.log("Mmmh... you should rotate your device to landscape");
} else if (orientation === undefined) {
console.log("The orientation API isn't supported in this browser :(");
}

C# Get Screen Orientation

You can take the SystemInformation-object instead and take its ScreenOrientation.

You will get a ScreenOrientation-enumartion back.

var orientation = SystemInformation.ScreenOrientation;
Console.WriteLine(orientation);

Prints 'Angle0' on my system with default orientation

Check orientation on Android phone

The current configuration, as used to determine which resources to retrieve, is available from the Resources' Configuration object:

getResources().getConfiguration().orientation;

You can check for orientation by looking at its value:

int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
// In landscape
} else {
// In portrait
}

More information can be found in the Android Developer.

How to detect orientation change in layout in Flutter?

In order to determine the Orientation of the screen, we can use the OrientationBuilder Widget. The OrientationBuilder will determine the current Orientation and rebuild when the Orientation changes.

new OrientationBuilder(
builder: (context, orientation) {
return new GridView.count(
// Create a grid with 2 columns in portrait mode, or 3 columns in
// landscape mode.
crossAxisCount: orientation == Orientation.portrait ? 2 : 3,
);
},
);

you can find the complete example here:
https://flutter.io/cookbook/design/orientation/

How to read current screen orientation on Android?

androidx.compose.ui.platform.LocalConfiguration.current.orientation

solved it for me.

Get the current screen orientation in MonoDroid

I found the answer written in Java here: How do I get the CURRENT orientation (ActivityInfo.SCREEN_ORIENTATION_*) of an Android device?

This is the corresponding MonoDroid C# translation:

private ScreenOrientation GetScreenOrientation() {
ScreenOrientation orientation;
SurfaceOrientation rotation = WindowManager.DefaultDisplay.Rotation;

DisplayMetrics dm = new DisplayMetrics();
WindowManager.DefaultDisplay.GetMetrics(dm);

if ((rotation == SurfaceOrientation.Rotation0 || rotation == SurfaceOrientation.Rotation180) && dm.HeightPixels > dm.WidthPixels
|| (rotation == SurfaceOrientation.Rotation90 || rotation == SurfaceOrientation.Rotation270) && dm.WidthPixels > dm.HeightPixels) {
// The device's natural orientation is portrait
switch (rotation) {
case SurfaceOrientation.Rotation0:
orientation = ScreenOrientation.Portrait;
break;
case SurfaceOrientation.Rotation90:
orientation = ScreenOrientation.Landscape;
break;
case SurfaceOrientation.Rotation180:
orientation = ScreenOrientation.ReversePortrait;
break;
case SurfaceOrientation.Rotation270:
orientation = ScreenOrientation.ReverseLandscape;
break;
default:
orientation = ScreenOrientation.Portrait;
break;
}
} else {
// The device's natural orientation is landscape or if the device is square
switch (rotation) {
case SurfaceOrientation.Rotation0:
orientation = ScreenOrientation.Landscape;
break;
case SurfaceOrientation.Rotation90:
orientation = ScreenOrientation.Portrait;
break;
case SurfaceOrientation.Rotation180:
orientation = ScreenOrientation.ReverseLandscape;
break;
case SurfaceOrientation.Rotation270:
orientation = ScreenOrientation.ReversePortrait;
break;
default:
orientation = ScreenOrientation.Landscape;
break;
}
}

return orientation;
}

Pretty much code for such a simple and common task, but it works perfectly.



Related Topics



Leave a reply



Submit