0

I would like to lock the screen orientation specifying the configuration in the AndroidManifest, instead of doing it programmatically. So I've ended up with the following approach:

values/config.xml

<resources>
    <integer name="orientation">1</integer>
</resources>

values-sw600dp/config.xml

<resources>
    <integer name="orientation">0</integer>
</resources>

If I check the value’s resource programmatically

getResources().getInteger(R.integer.orientation)

I get the expected value: 0 for tablets and 1 for handsets, which is the value specified by the framework for landscape and portrait orientation respectively.

But if I use this resource in the AndroidManifest:

<activity
            android:name="activities.InitialConfigActivity_"
            android:noHistory="true"
            android:screenOrientation="@integer/orientation" />

The activity always launches in portrait mode, regardless if it is a tablet or a handset device.

Any thoughts?

Thanks!

Víctor Albertos
  • 8,093
  • 5
  • 43
  • 71

1 Answers1

2

Edit: I originally read your question to mean you wanted complete control over the start-up orientation. If you are satisfied to get the natural/default orientation, then use nosensor as described below.


This may not qualify as an answer--I had more to share than could be put in a comment.

You've probably already looked at the discussion in these related issues: related-1, related-2, related-3.

In my experience, when an issue has been investigated by a number of different people for a period of four years, with no solution found, it usually means there is no solution.

One option might be to use the nosensor orientation in your manifest:

android:screenOrientation="nosensor"

This starts the activity in the device's "default" or "natural" orientation. On my devices this is: phone=portrait, 7-inch-tablet=portrait, 10-inch-tablet=landscape. Won't work for you if you want landscape for 7-inch tablet.

This post includes code for determining what a device's default orientation is.

Community
  • 1
  • 1
Bob Snyder
  • 37,759
  • 6
  • 111
  • 158