I donβt think there is a way to implement the backup mechanism in the manifest itself.
I would suggest that you specify one of the {userLandscape, sensorLandscape, landscape} in the manifest. Then check the version at runtime and improvise.
Let's say you decide to go with android:screenOrientation="userLandscape" in the manifest.
In your onCreate(Bundle) activity before setting up the content:
int sdkInt = Build.VERSION.SDK_INT;
Hope someone comes up with a better solution than this. It seems brute force.
Edit:
I tried a different approach - from what I know (and I could be wrong here), enums such as userLandscape , sensorLandscape , etc. will not change the value. As they currently stand:
<attr name="screenOrientation"> <enum name="unspecified" value="-1" /> <enum name="landscape" value="0" /> <enum name="portrait" value="1" /> <enum name="user" value="2" /> <enum name="behind" value="3" /> <enum name="sensor" value="4" /> <enum name="nosensor" value="5" /> <enum name="sensorLandscape" value="6" /> <enum name="sensorPortrait" value="7" /> <enum name="reverseLandscape" value="8" /> <enum name="reversePortrait" value="9" /> <enum name="fullSensor" value="10" /> <enum name="userLandscape" value="11" /> <enum name="userPortrait" value="12" /> <enum name="fullUser" value="13" /> <enum name="locked" value="14" /> </attr>
So, if you have to define an integer , for example:
<integer name="customScreenOrientation">0</integer> <integer name="customScreenOrientation">6</integer> <integer name="customScreenOrientation">11</integer>
Then you can use @integer/customScreenOrientation as the value for android:screenOrientation in your activity tag.
Of course, at best itβs a hack. If someone can confirm the stable status of the enumeration values ββfor screenOrientation , this can be a viable workaround - it is preferable to include the code from my previous sentence in several actions.
Another edit:
The second approach I mentioned earlier can be improved:
Instead of multiple integers.xml files integers.xml create 3 styles.xml files. I think you already have one - values/styles.xml . Create values-v9/styles.xml and values-v18/styles.xml .
<style name="AppTheme" parent="@style/BaseTheme"> <item name="android:screenOrientation">landscape</item> </style> <style name="AppTheme" parent="@style/BaseTheme"> <item name="android:screenOrientation">sensorLandscape</item> </style> <style name="AppTheme" parent="@style/BaseTheme"> <item name="android:screenOrientation">userLandscape</item> </style>
After that, create values/integers.xml (single file) and define an integer customScreenOrientation :
<integer name="customScreenOrientation">?android:attr/screenOrientation</integer>
The activity tag will look like this:
<activity .... android:theme="@style/AppTheme" android:screenOrientation="@integer/customScreenOrientation"/>
The advantage of this approach in the second is that we use enumerations instead of hard-coded values. Again, these two approaches are equivalent if enum values ββare given in stone. If they change, the second approach will fail while the third continues to move.