Description
I have an activity which displays the camera preview on a surface view. I do not want it to be restarted on orientation changes (as its constantly scanning for QR codes), so the orientation is locked in portrait.
The problem I have is that I need to display instructions on screen to the user at the bottom of the screen. Given the screen is locked in one orientation I do not get notified of the new orientation as the activity is not redrawn. Thus the instructions were rendered in the same place regardless of the phone been in portrait, reversed portrait, landscape or revered landscape.
To address this problem, I hooked up to the sensor manager to read in the orientation in realtime. (See code below) After reading it in I work out the orientation myself and move my instructions as required as each update is sent to me. This has worked for me so far until recently. When testing it on a Asus Transformer Prime and Xoom tablets. The tablet is returning the orientation in as "90" for portrait, instead of "0" like my other 2 phones. So as you can see in the code below, I check for tablets (large screens) and minus the extra 90 degrees.
Problem
The problem is the new Nexus 7 tablet does not have this extra bias. In portrait it returns "0", but I detect it as a tablet, so minus 90 degree thus the result is 270 and it places my instructions as if the tablet was in landscape mode. I assume the reasoning behind this is the Xoom/Transformer was designed to be used in landscape, and the Nexus in portrait, but unless I know about all the devices affected this doesn't help.
Question
Is there a reliable way to detect the orientation across all devices and return the results in "realtime", as in the code below, but taking into accoutn the default orientation of the device (i.e. Phones + Nexus 7 are portrait, but most tablets are landscape)?
Current Code
boolean large = ((getBaseContext().getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
boolean xlarge = ((getBaseContext().getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
isTablet = large || xlarge;
myOrientationEventListener = new OrientationEventListener(getBaseContext(), SensorManager.SENSOR_DELAY_NORMAL)
{
@Override
public void onOrientationChanged(int orientation)
{
if (orientation == -1)
{
return;
}
if (isTablet)
{
orientation += -90;
if (orientation < 0) // keep the result between 0-360
{
orientation += 360;
}
}
// Work out the orientation
if (orientation >= 60 && orientation <= 140)
{
screenOrientation = ScreenOrientation.Landscape_Reversed;
}
else if (orientation >= 140 && orientation <= 220)
{
screenOrientation = ScreenOrientation.Portrait_Reversed;
}
else if (orientation >= 220 && orientation <= 300)
{
screenOrientation = ScreenOrientation.Landscape;
}
else
{
screenOrientation = ScreenOrientation.Portrait;
}
//... Do stuff with new orientation here
}
};
Fixed it
Replaced the code
if (isTablet)
{
orientation += -90;
if (orientation < 0) // keep the result between 0-360
{
orientation += 360;
}
}
With the following
//Check "normal" screen orientation and adjust accordingly
int naturalOrientation = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
if (naturalOrientation == Surface.ROTATION_0)
{
Logging.d(TAG, "Rotation ROTATION_0");
}
else if (naturalOrientation == Surface.ROTATION_90)
{
Logging.d(TAG, "Rotation ROTATION_90");
orientation += 90;
}
else if (naturalOrientation == Surface.ROTATION_180)
{
Logging.d(TAG, "Rotation ROTATION_180");
orientation += 180;
}
else if (naturalOrientation == Surface.ROTATION_270)
{
Logging.d(TAG, "Rotation ROTATION_270");
orientation += 270;
}
if (orientation > 360) // Check if we have gone too far forward with rotation adjustment, keep the result between 0-360
{
orientation -= 360;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With