1
votes

Our app forces portrait or landscape mode based on screen size. Our layouts in the layout directory are for a default orientation of landscape. We have alternates as appropriate in layout-port. At start up, the app looks at the screen size and then sets the appropriate orientation. For example, to force portrait mode, we call:

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

For one fragment, we have a special layout for portrait mode that lives in layout-port.

If the device, a tablet, is currently resting in a landscape position, the default layout in the layout directory is chosen. This seems contrary to what should happen.

Is there a way when we are forcing portrait mode that we can ensure the xml file in the layout-port directory is used?

3

3 Answers

1
votes

You're just using the wrong flag, so it's not really forcing portrait mode. From the docs, SCREEN_ORIENTATION_SENSOR_PORTRAIT:

Would like to have the screen in portrait orientation, but can use the sensor to change which direction the screen is facing.

If you want to force portrait, use SCREEN_ORIENTATION_PORTRAIT:

Would like to have the screen in a portrait orientation: that is, with the display taller than it is wide, ignoring sensor data.

0
votes

I think the problem arises when values are checked before the portrait orientation change comes into effect, since the orientation is being defined in code, with something like this

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Anything that preceeds this in the code will not be aware of the change. I have noticed that calling setRequestOrientation can cause the layout to redraw, so you should wait till the redraw has finished before checking the value.

I noticed this because I had a similar problem and observed that landscape layout was being drawn before the portrait layout. The landscape layout did not contain all the elements of the portrait layout so it was triggering a null pointer exception. However, if I waited until the portrait redraw had finished, and then checked - the values were correct for portrait. This can safely be done in the onResume callback. Hope this helps.

-1
votes

As far as I know portrait mode is default. You should place your portrait layouts in the 'layout' folder and your landscape layouts in your 'layout-land' folder