CapitanNerd CapitanNerd - 6 months ago 35
Android Question

Set landscape orientation without locking sensor orientation

I'm working on an application that has an inline video player. I need to reproduce the fullscreen behavior of Youtube.

When the user goes to landscape rotating the device I catch it via onConfigurationChanged and make the corresponding layout modifications.

However, I also have a button to set fullscreen. I need this button to go to landscpe (ergo, fullscreen) but don't lock the orientation change via sensor.

Here is what I'm currently doing on onConfigurationChanged:

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (this.isLandscape(newConfig.orientation)){
this.goToFullScreenMode();
}else{
this.goToInlineMode();
}
}


And here is what I'm doing in the button but failing because it locks the Activity in landscape:

this.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);


What I need is to go to landscape mode but allow the user to change back to portrait with the sensor, like Youtube does.

I can do it by just locking in landscape until the device is horizontal (by reading directly from the sensor) and then unlock the orientation again. I don't think this is the most "correct" way of doing it.

Answer

Greetings from the futuristic year of 2016.

The paths of life took me back to this same predicament on another Android application and I found out that the REAL answer to the problem was to simply add this...

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

...right after setting the orientation to the desired setting.


Here lies my own previous answer

Due to a lack of answers and because I was not abale to solve this in an elegant way I finally ended using an OrientationListener to get the raw degrees of the device.

With that I was able to release the user's orientation configuration once the device was positioned like the actual layout.

If someone is interested in having more info about it just let me know.