Description
I have an activity that displays a preview of the camera on the surface. I do not want it to be restarted when the orientation changes (since it constantly scans QR codes), so the orientation is locked in the portrait.
The problem is that I need to display the instructions on the screen for the user at the bottom of the screen. Given that the screen is locked in one orientation, I do not receive a notification about the new orientation, since the activity is not redrawn. Thus, the instructions were presented in the same place, regardless of whether the phone was made in portrait, reverse portrait, landscape or reverence.
To solve this problem, I connected to the sensor manager to read in real-time orientation. (See Code below). After reading it, I myself will develop an orientation and translate my instructions as necessary, when each update is sent to me. This still worked for me until recently. When tested on Asus Transformer Prime and Xoom tablets. The tablet returns the orientation as "90" for the portrait, not "0", like my other 2 phones. So, as you can see in the code below, I am testing tablets (large screens) and minus an additional 90 degrees.
Problem
The problem is that the new Nexus 7 tablet does not have this extra bias. In the portrait, it returns “0”, but I find it as a tablet, so minus 90 degrees, the result is 270, and it sets my instructions as if the tablet was in landscape mode. I assume that the argument for this is that the Xoom / Transformer was designed for use in the landscape, and the Nexus in the portrait, but if I don’t know about all the devices it affects, this does not help.
Question
Is there a reliable way to determine the orientation on all devices and return the results in "real time", as in the code below, but taking into account the default orientation of the device (for example, phones + Nexus 7 are portraits, but most tables 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)
{
orientation += 360;
}
}
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;
}
}
};