I would like to find out the detailed orientation of a device, preferably one of SCREEN_ORIENTATION_LANDSCAPE
, SCREEN_ORIENTATION_PORTRAIT
, S
I ended up using Zoltán's answer above, which works great, except when I tried it on a tablet (a Samsung P6210 Galaxy Tab 7.0 Plus). In portrait mode, it returned SCREEN_ORIENTATION_REVERSE_PORTRAIT. So in the else statement (if natural orientation is landscape) I swapped the cases for ROTATION_90 and ROTATION_270, and everything seems to work fine. (I don't have enough reputation to post this as a comment to Zoltán's answer.)
You could do it in a very simple way:
get the screen widht
and height
.
screen width will be always higher when device is in landscape orientation.
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Toast.makeText(getApplicationContext(), "" + width + "," + height,
Toast.LENGTH_SHORT).show();
if (width > height) {
Toast.makeText(getApplicationContext(), "LandScape",
Toast.LENGTH_SHORT).show();
}
Simple approach would be to use
getResources().getConfiguration().orientation
1 is for Potrait and 2 for Landscape.
I think your problem is that you can detect landscape and portrait but not reverse landscape and reverse protrait as they are not supported in older versions. To detect what you can do is that you can use both oreintation and rotation. I am giving you an idea it may be useful for you.
try this i think it may solve your problem.
int orientation = getResources().getConfiguration().orientation;
int rotation = getWindowManager().getDefaultDisplay().getRotation();
int actual_orientation = -1;
if (orientation == Configuration.ORIENTATION_LANDSCAPE
&& (rotation == Surface.ROTATION_0
|| rotation == Surface.ROTATION_90)){
orientation = Configuration.ORIENTATION_LANDSCAPE;
} else if (orientation == Configuration.ORIENTATION_PORTRAIT
&& (rotation == Surface.ROTATION_0
|| rotation == Surface.ROTATION_90)) {
orientation = Configuration.ORIENTATION_PORTRAIT;
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE
&& (rotation == Surface.ROTATION_180
|| rotation == Surface.ROTATION_270)){
orientation = //any constant for reverse landscape orientation;
} else {
if (orientation == Configuration.ORIENTATION_PORTRAIT
&& (rotation == Surface.ROTATION_180
|| rotation == Surface.ROTATION_270)){
orientation = //any constant for reverse portrait orientation;
}
}
getResources().getConfiguration().orientation
is the standard way of knowing current orientation being used. However, if it doesn't fulfill your needs then perhaps you may use Sensors to calculate it in terms of angle. Read this and this
public static int getScreenOrientation(Activity activity) {
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int orientation = activity.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270) {
return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
} else {
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
}
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {
return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
} else {
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
}
}
return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
}