Android force orientation

You must Login before you can answer or comment on any questions.

Not really a question, but a note to other users.

I've seen some posts asking how to force an app into landscape only or portrait only, but the answers there weren't working for me. I finally got it working, so I thought I would post how I did it:

1)create a file in <app>/build/android called AndroidManifest.custom.xml

2)copy all content from AndroidManifest.xml into new custom file.

3)for every activity node, add the following line: android:screenOrientation="portrait" If you want landscape do that instead, if you want both you can do "portrait|landscape"

4)(the key that was tripping me up) again for each activity node, REMOVE "|orientation" from the android:configChanges section.

after rebuilding, you should have an app locked into the orientation of your choice

— asked 2 years ago by Jason Harris
9 Comments
  • Thanks a lot for sharing!!

    — commented 2 years ago by Jordan Bonnet

  • Awesome, I've been banging my head against the wall trying to figure this out!

    — commented 2 years ago by Jason E. Gyurkovitz

  • This has been really irritating and annoying for me as well, but this solution works great! Thanks Jason, you're a saviour.

    — commented 1 year ago by Boydlee Pollentine

  • Show 6 more comments

15 Answers

another way programmatically to force portrait to all views

if (Ti.Platform.osname == 'android'){
            Ti.Gesture.addEventListener('orientationchange', function(e) {
 
              Ti.Android.currentActivity.setRequestedOrientation(Ti.Android.SCREEN_ORIENTATION_PORTRAIT);
            });
        }

— answered 7 months ago by Hosam Redwan
answer permalink
8 Comments
  • +1!! This is the only solution that would work for me, thanks!

    — commented 6 months ago by Elijah Mirecki

  • Fixed for me too, and this is recent. Vote this up for heaven's sake!

    — commented 6 months ago by Sky Apperley

  • Yes, this is recent one and Fixed for me too.. Thanks a lot!!

    — commented 6 months ago by Sachin Dharne

  • Show 5 more comments

To force orientation to be 'portrait' only add following code in tiapp.xml

<android >
        <manifest>
            <application>
                <activity android:name="com.myActivity" android:screenOrientation="portrait"
                    android:configChanges="keyboardHidden|orientation"
                    >
                </activity>
            </application>
        </manifest>
    </android>

— answered 10 months ago by p. l.
answer permalink
3 Comments
  • Note: In the case of titanium set

    android:name="org.appcelerator.titanium.TiActivity".

    — commented 10 months ago by p. l.

  • doesn't work

    — commented 9 months ago by Jason B

  • This worked for me

    <android . . . >
        . . .
        <manifest . . . >
            . . .
            <application>
                <activity android:name="org.appcelerator.titanium.TiActivity" 
                    android:screenOrientation="portrait" 
                    android:configChanges="keyboardHidden|orientation">
                </activity>
            </application>
        </manifest>
    </android>
    but I ended up using what "d b" suggested:
    var win = Ti.UI.createWindow();
    win.orientationModes=[Titanium.UI.PORTRAIT];
    for more granular control.

    — commented 8 months ago by Dan Frist

for some reason it looks like

var win = Ti.UI.createWindow({orientationModes:[Titanium.UI.PORTRAIT]});
only works on iPhone, but for android you have to do
var win = Ti.UI.createWindow();
win.orientationModes=[Titanium.UI.PORTRAIT];
this works perfectly now in 1.7.2

— answered 9 months ago by d b
answer permalink
3 Comments
  • It's so weird that this works, but it does! Thanks.

    — commented 8 months ago by Dan Frist

  • I'll second that :) - And big thanks for the solution!

    — commented 8 months ago by Fredrik Motin

  • Great post! Big thanks from me too!

    — commented 7 months ago by Ă…se Dittlau

Ok, what I don't understand is that Titanium.UI.orientation = Titanium.UI.LANDSCAPE_LEFT seems to work in KitchenSink, although it results in landscape RIGHT for some reason...

Well, i'm running the same issue, but after a lot of testing this solution almost work fine, almost because in the landscape mode the app show the splash screen many times... This was testing over the emulator and over an android 2.1

1) Create a custom AndroidManifest.xml (copy this from original generated source "build/android/AndroidManifest.xml")

2) Copy this on the created folder "platform/android/AndroidManifest.xml" at the top level of your project.

3) Add this: android:screenOrientation="portrait" to all activities

4) remove this: "|orientation" from all activities.

Here is my AndroidManifest.xml.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.patagonialabs.mytest" android:versionCode="1"
    android:versionName="1">
    <uses-sdk android:minSdkVersion="7" />
 
    <!-- TI_MANIFEST -->
 
    <application android:icon="@drawable/appicon"
         android:label="Mytest" android:name="MytestApplication"
        android:debuggable="false">
 
        <!-- TI_APPLICATION -->
 
        <activity android:name=".MytestActivity"
            android:label="Mytest" android:theme="@style/Theme.Titanium"
            android:configChanges="keyboardHidden"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
 
 
        <activity android:name="org.appcelerator.titanium.TiActivity"
            android:configChanges="keyboardHidden" 
            android:screenOrientation="portrait"/>
        <activity android:name="org.appcelerator.titanium.TiTranslucentActivity"
            android:configChanges="keyboardHidden"
            android:theme="@android:style/Theme.Translucent" 
            android:screenOrientation="portrait"/>
        <activity android:name="org.appcelerator.titanium.TiModalActivity"
            android:configChanges="keyboardHidden"
            android:theme="@android:style/Theme.Translucent"
            android:screenOrientation="portrait"/>
        <activity android:name="ti.modules.titanium.ui.TiTabActivity"
            android:configChanges="keyboardHidden" 
            android:screenOrientation="portrait"/>
        <activity android:name="ti.modules.titanium.ui.android.TiPreferencesActivity" 
            android:screenOrientation="portrait" />
        <service android:name="org.appcelerator.titanium.analytics.TiAnalyticsService"
            android:exported="false"
            android:screenOrientation="portrait"/>
 
 
    </application>
 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
 
 
</manifest>

— answered 9 months ago by Giancarlo Rotta
answer permalink
2 Comments
  • I tried all this. When I remove the "|orientation" part, the app crashes every time when I test it on a device. If I leave it in, the orientation is not locked.

    — commented 9 months ago by Fredrik Søgaard

  • Fredrick, as I said that solution almost work, but my app don't crash, this only show the splash screen in the landscape orintation. But i got a working solution (and tested in the device). Really I don't know if is a combination of the previously AndroidManifes.xml plus this lines, but it works. In all my windows I have this:

    var win             = Ti.UI.currentWindow;
    win.orientationModes = [  
        Titanium.UI.PORTRAIT
    ];
    I hop this work for you too, finally I will publish my android app today!

    — commented 9 months ago by Giancarlo Rotta

That tip made my day! (i'm working with 1.7.0 and still have the issue)

http://developer.appcelerator.com/question/103611/how-do-i-set-androidversioncode-and-androidversionname

Rather than doing the AndroidManifest.custom.xml (which will fail if you try to build with one), just take all of your <manifest> code and dump that into tiapp.xml. It works for me on 1.7.2!

— answered 11 months ago by Andrew Mussey
answer permalink
1 Comment
  • What are you supposed to put in tiapp.xml to get it to work though? I tried:

    <android >
      <manifest android:screenOrientation="landscape"></manifest>
    </android>
    But it doesn't seem to work.

    — commented 10 months ago by Todd Wade

Does forcing portrait in Android work yet? If so...does anyone have some code they can share with me.

thanks in advance!

in tiapp.xml

according to your starting view you should add the following to the equivalent activity tag

for example if you use TabGroup as a main view use the following

<android xmlns:android="http://schemas.android.com/apk/res/android">
        <manifest>
            <application>
    <activity android:name="ti.modules.titanium.ui.TiTabActivity"
            android:configChanges="keyboardHidden|orientation"  
 android:screenOrientation="portrait"/>
 
 
 
            </application>
        </manifest>
    </android>

so you should review frist the starting main view activity from AndroidManifest.xml under the build folder

and copy the activity tag as is then add the following attribute to it android:screenOrientation="portrait"

then add it as above to tiapp.xml

This problem seems to have reappeared with SDK 1.7.0

how about if you just want to force certain Android gui's to be in Portrait ONLY?

I'm looking at the manifest XML file, but what if i have certain screens that should be locked to portrait and others that should be locked to landscape? can i add a new activity for those windows? how do i make sure those windows are connecting to that information?

try this in yourFile.js

win.orientationModes = [Ti.UI.PORTRAIT];

Hey guys. This should be simple, but I really can't get my Android app to be forced in portrait mode. I've tried every trick I've found on the forums. Please help!

FYI - The orientation issue on Android has been fixed and should be in the next release of Titanium :)

Your Answer

Think you can help? Login to answer this question!