How to Get the Current Orientation (Activityinfo.Screen_Orientation_*) of an Android Device

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.

I want to know the screen orientation of the phone

int orientation = this.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
// code for portrait mode
} else {
// code for landscape mode
}

Answer from

and check this answer for knowing the rotation

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.

Android get Device orientation

Use Orientation change listener

OrientationEventListener mOrientatinChangeListener = new OrientationEventListener(getApplicationContext(), SensorManager.SENSOR_DELAY_NORMAL) {

@Override
public void onOrientationChanged(int orientation) {
// Here you will get the actual orientation
}

};

protected void onStart() {
super.onStart();

// Activate the Orientation change listener
mOrientatinChangeListener.enable();
}

Note : If you are expecting a 3 dimensional orientation from gyroscope/ accellerometer then Using Android gyroscope instead of accelerometer. I find lots of bits and pieces, but no complete code

Getting the natural orientation of a device in CN1

I don't think there is a native API for getting the natural device orientation in any OS. Regardless, iOS only allows you to lock device orientation properly with a build hint so that wouldn't be very useful.

Get default device orientation (Landscape or Portrait) on Android/iOS

Actually found what I needed by myself !

Here's the link, for anyone interested : https://gist.github.com/flarb/3252857

(I didn't need the iOS check in the end, since our application is only supported on tablets anyway, plus I think the default orientation on all iOS devices is Portrait (might be wrong))

Cheers !

How can I get the current screen orientation?

Activity.getResources().getConfiguration().orientation

Get phone orientation but fix screen orientation to portrait

Could you satisfy your requirement with the accelerometer? If so, perhaps something like this (untested) fragment would suit your purposes.

SensorManager sensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
sensorManager.registerListener(new SensorEventListener() {
int orientation=-1;;

@Override
public void onSensorChanged(SensorEvent event) {
if (event.values[1]<6.5 && event.values[1]>-6.5) {
if (orientation!=1) {
Log.d("Sensor", "Landscape");
}
orientation=1;
} else {
if (orientation!=0) {
Log.d("Sensor", "Portrait");
}
orientation=0;
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub

}
}, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);


Related Topics



Leave a reply



Submit