Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Android force orientation

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 July 1st 2010 by Jason Harris
  • android
  • orientation
  • tips
14 Comments
  • Thanks a lot for sharing!!

    — commented September 11th 2010 by Jordan Bonnet
  • Awesome, I've been banging my head against the wall trying to figure this out!

    — commented November 1st 2010 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 April 20th 2011 by Boydlee Pollentine
  • Helped me out, thanks!

    — commented May 20th 2011 by Jeff Cross
  • works great, thanks

    — commented July 20th 2011 by Nary Razafintsalama
  • Hi Jason! I tried your method and FINALLY, seriously, I mean finally, the screen is locked, but when I tilt the phone, the app seems to try to refresh the layout, and then after several times, it collapsed. Any way to solve this?

    — commented August 9th 2011 by Sihang H
  • worked for me ….. thank you …

    — commented November 8th 2011 by juliana cora
  • Great Jason……its pull me out from a very big headache…..:-)

    — commented November 18th 2011 by Mathew Varghese
  • I'm going to post the comment on here and on the other highly voted answer- in case it would help someone out.

    I originally had this working by trapping the Gesture event orientationChange, which is documented below. Then because I needed to trap the "android:back" event I had to move to a heavyweight window for my main base window. When I did this the orientationChange event solution STOPPED working.

    After going through the Q&A I decided to give this a whirl. So I added the lines above to a custom manifest and it works fine again. So the heads up from me is that I had to use the custom manifest documented above in order to get the orientation to lock in place when I used a heavyweight window as the base window. I can't think of anything else that I changed when the original solution stopped working.

    — commented January 22nd 2012 by Ben Scofield
  • Thanks so much for sharing this … I've tried all the other answers below and this is the only one that works for me!

    — commented September 25th 2012 by Manus Du Plessis
  • Can anyone state if the custom version of the xml file will need to be regenerated (copied and edited) if you make any changes to tiapp.xml, or only <android> section changes, or under any other circumstances (e.g., "clear the build dir and try again", which we do a lot)? Does the AndroidManifest.xml still get read before the custom version, so you can strip out other content leaving just the hierarchy down to the <activity> tags? When working with a team of developers won't we need to copy the custom xml around all the time? Is there no way to include this in the tiapp.xml?
    Thanks!

    — commented November 14th 2012 by Richard Baughman
  • @Richard Baughman
    The AndroidManifest.custom.xml file under build/android is now deprecated. Good news: you can now have it to be built everytime. Make the a custom AndroidManifest file like Jason Harris tell to do, but place it under <app>/platform/android/ instead, and name it AndroidManifest.xml. Create the folders /platfrom/android if necessary. That worked for me.

    — commented November 19th 2012 by Vincent Fortier
  • Worked like butter for me :) Thanks Jason

    — commented April 24th 2013 by Abhishek Sharma
  • you're my favourite, you're my saviours

    ps. ppl put it on <app>\platform\android with the original AndroidManifest.xml name now.

    — commented May 15th 2015 by Danilo Ganzella

20 Answers

  • Accepted Answer

    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 November 8th 2011 by Hosam Redwan
    permalink
    13 Comments
    • +1!! This is the only solution that would work for me, thanks!

      — commented November 18th 2011 by Elijah Mirecki
    • Fixed for me too, and this is recent. Vote this up for heaven's sake!

      — commented December 1st 2011 by Sky Apperley
    • Yes, this is recent one and Fixed for me too.. Thanks a lot!!

      — commented December 2nd 2011 by Sachin Dharne
    • Oh My God. Thanks. Finally solved my orientation issues when every other solution failed.

      — commented December 3rd 2011 by Emily Toop
    • This worked for me :) Thanks Hosam!

      — commented December 10th 2011 by Marion Dorsett
    • Good one, thanks !

      — commented December 13th 2011 by shubham saurabh
    • Hmm, this solution is not foolproof. If you start the app already in landscape mode then obviously the 'orientationchange' event won't fire. Also tried placing Ti.Android.currentActivity.setRequestedOrientation(Ti.Android.SCREEN_ORIENTATION_PORTRAIT); before and/or after opening the main window and it wouldn't work.

      Also fails when opening a new window, again already in landscape. So use this solution with caution!

      — commented December 15th 2011 by Ivan Aguirre
    • I'm going to post the comment on here and on the other highly voted answer- in case it would help someone out.

      I originally had this working by trapping the Gesture event orientationChange, which is documented here. Then because I needed to trap the "android:back" event I had to move to a heavyweight window for my main base window. When I did this the orientationChange event solution STOPPED working.

      After going through the Q&A I decided to give the custom manifest a whirl. So I added the lines mentioned in the first post to the custom manifest and it works fine again.

      So the heads up from me is that I had to use the custom manifest documented above in order to get the orientation to lock in place when I used a heavyweight window as the base window. I can't think of anything else that I changed when the original solution stopped working.

      — commented January 22nd 2012 by Ben Scofield
    • helpful info

      — commented November 2nd 2012 by Ashutosh Chaturvedi
    • to load the window as portrait just add

      Ti.Android.currentActivity.setRequestedOrientation(Ti.Android.SCREEN_ORIENTATION_PORTRAIT);
      

      in app.js before the createWindow and without the orientationchange addEventListener

      — commented January 28th 2013 by sameeh harfoush
    • Where to write this code?

      — commented May 24th 2013 by Syed Aqeel
    • works like a charm! Thumbs up for the 3 lines of code solution!

      — commented November 13th 2013 by Carlos Henrique Lustosa Zinato
    • Thank you .. its working ..

      — commented December 27th 2013 by phani kumar
  • 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 July 29th 2011 by Titanium Developer
    permalink
    6 Comments
    • Note: In the case of titanium set

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

      — commented July 29th 2011 by Titanium Developer
    • doesn't work

      — commented August 30th 2011 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 September 28th 2011 by Dan Frist
    • None of these work for me :(

      — commented July 23rd 2012 by Marco Seguri
    • Dan Frist and P L's solution worked for me in TiApp.xml.

      — commented November 13th 2012 by Chris Colborne
    • For Android only works for me this

      var win = Ti.UI.createWindow();
      win.orientationModes=[Titanium.UI.PORTRAIT];

      — commented June 5th 2014 by Luis Alberto Hernandez
  • 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 September 6th 2011 by Giancarlo Rotta
    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 September 7th 2011 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 September 14th 2011 by Giancarlo Rotta
  • 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 September 14th 2011 by d b
    permalink
    4 Comments
    • It's so weird that this works, but it does! Thanks.

      — commented September 28th 2011 by Dan Frist
    • I'll second that :) - And big thanks for the solution!

      — commented October 6th 2011 by Fredrik Motin
    • Great post! Big thanks from me too!

      — commented November 4th 2011 by Åse Dittlau
    • Doesn't work for windows created inside tabgroups…

      — commented July 23rd 2012 by Marco Seguri
  • You need to set the orientation of the activity in the manifest.

    Setting this in the code as suggested with win.orientationModes = [Ti.UI.PORTRAIT]; will only kick in once the app has loaded, so the splash screen can still rotate, and occasionally I have seen the app start in portrait, but report Ti.Platform.displayCaps.platformWidth and Ti.Platform.displayCaps.platformHeightswitched., causing it to be half off-screen.

    Hard part about this is to do this is to obtain the activity name. Build the app, then examine the AndroidManifest.xml file in the build&#x2F;android folder. Mine looks something like this:

    <?xml version="1.0" ?>
    <manifest android:versionCode="1" android:versionName="1" package="com.example.testapp" xmlns:android="http://schemas.android.com/apk/res/android">
        <uses-sdk android:minSdkVersion="8"/>
    
        <application android:debuggable="false" android:icon="@drawable/appicon" android:label="Example App" android:name="ExampleAppApplication">
    
    
    <!-- IMPORTANT PART STARTS HERE -->
            <activity android:configChanges="keyboardHidden|orientation" android:label="Example App" android:name=".ExampleAppActivity" android:theme="@style/Theme.Titanium">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
    
            <activity android:configChanges="keyboardHidden|orientation" android:name="org.appcelerator.titanium.TiActivity" />
    
    <!-- IMPORTANT PART ENDS HERE -->
    
            <activity android:configChanges="keyboardHidden|orientation" android:name="ti.modules.titanium.media.TiCameraActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"/>
    
            <activity android:configChanges="keyboardHidden|orientation" android:name="org.appcelerator.titanium.TiTranslucentActivity" android:theme="@android:style/Theme.Translucent"/>
            <activity android:configChanges="keyboardHidden|orientation" android:name="org.appcelerator.titanium.TiModalActivity" android:theme="@android:style/Theme.Translucent"/>
            <activity android:configChanges="keyboardHidden|orientation" android:name="ti.modules.titanium.ui.TiTabActivity"/>
            <activity android:name="ti.modules.titanium.ui.android.TiPreferencesActivity"/>
    
            <service android:exported="false" android:name="org.appcelerator.titanium.analytics.TiAnalyticsService"/>
        </application>
    
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
        <uses-permission android:name="android.permission.CAMERA"/>
        <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>
    

    As you can see in the listing, the screenOrientation property has not been applied. You need to add this in your tiapp.xml. Unfortunately, if you simply add this to tiapp.xml:

    <activity android:name=".ExampleAppActivity" android:screenOrientation="portrait" />
    

    Titanium will not merge the correct output, but replace it with the half-implemented version (you will loose your launch icon for instance). To fix this you need to copy the block and change it.

    From:

            <activity android:configChanges="keyboardHidden|orientation" android:label="Example App" android:name=".ExampleAppActivity" android:theme="@style/Theme.Titanium">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
    
            <activity android:configChanges="keyboardHidden|orientation" android:name="org.appcelerator.titanium.TiActivity" />
    

    To (notice the added orientation="portrait" attributes)

            <activity android:configChanges="keyboardHidden|orientation" android:label="Example App" android:name=".ExampleAppActivity" android:theme="@style/Theme.Titanium" android:screenOrientation="portrait" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
    
            <activity android:configChanges="keyboardHidden|orientation" android:name="org.appcelerator.titanium.TiActivity" android:screenOrientation="portrait" />
    

    This needs to be placed in you tiapp.xml file under the application tag, like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <ti:app xmlns:ti="http://ti.appcelerator.org">
        ...
        <android xmlns:android="http://schemas.android.com/apk/res/android">
            <manifest>
                <application>
            <activity android:configChanges="keyboardHidden|orientation" android:label="Example App" android:name=".ExampleAppActivity" android:theme="@style/Theme.Titanium" android:screenOrientation="portrait" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
    
            <activity android:configChanges="keyboardHidden|orientation" android:name="org.appcelerator.titanium.TiActivity" android:screenOrientation="portrait" />
                </application>
            ...
            </manifest>
            ...
        </android>
        ...
    </ti:app>
    

    Once done, try building it again, and inspect build&#x2F;android&#x2F;AndroidManifest.xml, and you should now see that it has been updated correctly. You can use this same method if you have multiple activities that need different orientation.

    — answered January 7th 2013 by Kenneth Skovhede
    permalink
    2 Comments
    • Thank you so much! Was banging my head against the wall on this one. As Kenneth points out, forcing the app into portrait (or landscape) only takes effect after the loading screen passes and the actual app loads. This causes incorrect (switched) platform height and platform width variables.

      This is the only method that really worked for me, as my app is a percentage based layout and starting in landscape caused the containing views to have switched height/width variables.

      — commented October 8th 2013 by Nick Foti
    • It works! You are amazing!! :)

      — commented September 24th 2014 by Miguel Ángel Castaño Fajardo
  • 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

    — answered November 6th 2011 by Hosam Redwan
    permalink
    0 Comments
  • 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…

    — answered November 30th 2010 by Joel Cogen
    permalink
    0 Comments
  • 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 June 30th 2011 by Andrew Mussey
    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 July 27th 2011 by Todd Wade
  • This problem seems to have reappeared with SDK 1.7.0

    — answered April 25th 2011 by Caspar Addyman
    permalink
    2 Comments
    • Yeah, and I noticed density support is outta whack in 1.7.0, too.

      — commented May 20th 2011 by Jeff Cross
    • Yep!

      — commented May 27th 2011 by Anders Pedersen
  • That tip made my day! (i'm working with 1.7.0 and still have the issue)

    — answered June 21st 2011 by Greg Berger
    permalink
    0 Comments
  • Does forcing portrait in Android work yet? If so…does anyone have some code they can share with me.

    thanks in advance!

    — answered July 12th 2011 by Brian Dittmer
    permalink
    0 Comments
  • how about if you just want to force certain Android gui's to be in Portrait ONLY?

    — answered July 13th 2011 by Brian Dittmer
    permalink
    0 Comments
  • 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?

    — answered September 14th 2011 by d b
    permalink
    0 Comments
  • try this in yourFile.js

    
    win.orientationModes = [Ti.UI.PORTRAIT];
    
    — answered November 26th 2011 by Haya aziz
    permalink
    0 Comments
  • This is the only solution that worked for me:

    win.addEventListener('focus', function(e) {
        win.orientationModes = [Titanium.UI.PORTRAIT];
    });
    
    — answered July 23rd 2012 by Marco Seguri
    permalink
    0 Comments
  • HI I need portrait in android phone and landscape in tablet, how can we write..please anyone suggest .
    Thanks

    — answered October 7th 2013 by Prashaanth N.G.S
    permalink
    0 Comments
  • The issue lies with the name of the activity. Many people are using the default name which starts with a period. You should be using the base class name for the activity org.appcelerator.titanium.TiActivity.

    <activity
        android:name="org.appcelerator.titanium.TiActivity"
        android:screenOrientation="portrait"
        android:configChanges="keyboardHidden|orientation">
    </activity>
    
    — answered May 1st 2015 by Collin Price
    permalink
    0 Comments
  • win.addEventListener('focus', function(e) {
        win.orientationModes = [Titanium.UI.PORTRAIT];
    });
    

    This works but it is not the best way to do it.

    — answered November 17th 2012 by Michael Massalas
    permalink
    0 Comments
  • 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!

    — answered September 6th 2011 by Fredrik Søgaard
    permalink
    0 Comments
  • FYI - The orientation issue on Android has been fixed and should be in the next release of Titanium :)

    — answered July 1st 2010 by David Ashwood
    permalink
    5 Comments
    • Still an issue as of August 29, 2011

      — commented August 29th 2011 by Sean Colombo
    • Still an issue as of July 23, 2012

      — commented July 23rd 2012 by Marco Seguri
    • Still an issue as of January 9, 2013

      — commented January 9th 2013 by Marco Seguri
    • Still an issue

      — commented April 16th 2013 by Dhananjay Choudhari
    • Still an issue

      — commented July 19th 2013 by Matt Garland
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.