You have a few options for handling device orientation:
- Lock orientation for the whole app
- Lock the orientation for a given window
- React to orientation changes.
Before we begin, it’s important to cover some caveats and subtleties on Android. With that platform, it’s important that you keep in mind that the orientation values you set don’t match those you get. With Android, you can set the UI orientation to any of four possibilities: portrait upright, landscape right, portrait upside-down, and landscape left. But, when you request the current orientation, you’ll get one of two values: portrait or landscape. This is a platform feature, not a Titanium implementation issue.
A further consideration is that portrait and landscape vary between phones and tablets. A phone is in portrait mode when its “top” is at 0 degrees (hardware buttons at the bottom) and landscape when the “top” is at 270 degrees. A tablet is in landscape mode when its top is at 0 and portrait when its top is at 90 degrees. (Based on sensor degrees.) These portrait/landscape values are what you receive when you get the devices current orientation.
ORIENTATION DESIGN PRINCIPLES:
Apple’s Developer documentation says: “People expect to use your app in different orientations, and it’s best when you can fulfill that expectation.” In other words, don’t look at handling orientation as a bother but an opportunity.
Apple further recommends that when choosing to lock or support orientation, you should consider following these principles:
On iPhone/iPod Touch – don’t mix orientation of windows within a single app; so, either lock orientation for the whole app, or react to orientation changes.
On iPhone – don’t support the portrait-upside-down orientation, because that could leave the user with their phone upside-down when receiving a phone call.
On iPad – you should support all orientations because that matches how people use those devices.
These same principles apply to an Android app as well.
ORIENTATION IS AN OPPORTUNITY:
Rather than considering orientation a “necessary evil” to handle, think of it as an opportunity. When a user rotates their device, you could display different content. Consider a recipe app that shows a list of ingredients when in portrait mode but shows cooking directions when the device is in landscape mode. Some handsets mute the speaker when the device is face down. You can probably think of other interesting ways your app could react to an orientation change.
LIMITING ORIENTATION MODES SUPPORTED BY YOUR APP:
You specify the orientations your app can support by modifying the tiapp.xml file. This type of configuration controls the splash screen orientation possibilities. And it constrains which orientations the windows of your apps could possible show in, but not necessarily the orientation of a specific window.
The techniques for iOS and Android vary, so we’ll look at them separately.
LIMITING ORIENTATION MODES ON IOS:
On iOS, you specify the orientations your app supports with values in the tiapp.xml file. By removing an orientation from the list, you prevent the app from displaying in that way. For example, to support only portrait on the iPhone, but support all rotations on the iPad, you’d use this XML in your project’s tiapp.xml file:
CODE:
<iphone>
<orientations device="iphone">
<orientation>Ti.UI.PORTRAIT</orientation>
</orientations>
<orientations device="ipad">
<orientation>Ti.UI.PORTRAIT</orientation>
<orientation>Ti.UI.UPSIDE_PORTRAIT</orientation>
<orientation>Ti.UI.LANDSCAPE_LEFT</orientation>
<orientation>Ti.UI.LANDSCAPE_RIGHT</orientation>
</orientations>
</iphone>
LIMITING ORIENTATION MODES ON ANDROID:
CODE:
<android xmlns:android="http://schemas.android.com/apk/res/android">
<manifest>
<application>
<activity
android:name="org.appcelerator.titanium.TiActivity"
android:configChanges="keyboardHidden"
android:screenOrientation="portrait"
/>
<activity android:name="ti.modules.titanium.ui.TiTabActivity"
android:configChanges="keyboardHidden"
/>
</application>
</manifest>
</android>
“Try to be like the turtle – at ease in your own shell.”