Titanium Community Questions & Answer Archive

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

Get orientation on load

Hello,

I have some functions to run once my orientation changes, so I've made a:

Ti.Gesture.addEventListener('orientationchange', function(e){
   // blablabla
});

However, I also want to run that through on application startup, so I've made the following:

app.addEventListener('load', function(){
   // blablabla
   Ti.Gesture.fireEvent('orientationchange');
});

I noticed that since there is no orientation change, my e.orientation will equal null and the event will loop until the orientation changes.

So my question: Is there any way to get the orientation on startup? I could just assume it's portrait, but that would be cheating..

Caspar

— asked August 4th 2010 by Caspar Jespersen
  • gesture
  • iphone
  • orientation
0 Comments

5 Answers

  • On application start I am setting the orientation according to the platform width and height.

    // get max width and height
    var pWidth = Ti.Platform.displayCaps.platformWidth;
    var pHeight = Ti.Platform.displayCaps.platformHeight;
    
    if (pWidth > pHeight) {
        var oriCurrent = 'landscape';
    } else {
        var oriCurrent = 'portrait';    
    }
    Ti.API.info('Window orientation: ' + oriCurrent);
    
    — answered October 26th 2011 by Matthias Fliegner
    permalink
    2 Comments
    • This worked for me. I wanted to get the orientation on app load - tried a number of things - finally used your suggestion although I think its a "roundabout" way of doing things. That did the trick though!
      somehow I think, Ti.UI.orientation should tell you what the orientation is - but sadly it does not!
      Thanks Mathias!

      — commented March 15th 2012 by Sameer Prabhu
    • comparing platformWidth and platformHeight is also useful if you don't want to bother with FACE_UP and FACE_DOWN orientations

      — commented February 27th 2013 by Sebastiaan Hoogervorst
  • Orientation tracking is a painful task, there are two more occasions face up and down that i don't think casual app would needed unless you are developing a game. Another thing to check if the device is placed flat on the table with no orientation locked.

    What you can do is create a function to return orientation modes you needed, another to execute, then add events handler to trigger those functions, and do nothing or something if orientation is unknown.

    function getOrientation(o) {
        switch (o) {
            case Titanium.UI.PORTRAIT: {return 'portrait';}
            case Titanium.UI.UPSIDE_PORTRAIT: {return 'portrait';}
            case Titanium.UI.LANDSCAPE_LEFT: {return 'landscape';}
            case Titanium.UI.LANDSCAPE_RIGHT: {return 'landscape';}
            default : return 'unknown'
        }
    }
    
    Ti.Gesture.addEventListener('orientationchange',function(e){
        var orientation = getOrientation(e.orientation);
        if(orientation == '')
    ......
    
    })
    
    app.addEventListener('load', function(){
        var orientation = getOrientation(e.orientation);
        if(orientation == '')
    ......
    
    });
    
    — answered August 4th 2010 by Daniel Lim
    permalink
    2 Comments
    • Doesn't seem to work, it gives this error:

      "Result of expression 'Ti.Gesture' [undefined] is not an object.";

      — commented November 14th 2011 by Justin Toth
    • @Justin, that's probably has to do with Ti.Gesture is not included, try to clean up your build first.

      — commented June 19th 2012 by Lars C. Magnusson
  • I think you also have to check the orientation right at the launch off your App.

    I have solved that with the following:

    At the very end off my app.js

    if(Titanium.UI.orientation == Titanium.UI.PORTRAIT 
      || Titanium.UI.orientation == Titanium.UI.UPSIDE_PORTRAIT){
          // Function that changes the left property of 
          // just about every item in the view
          app.config.rotatePortrait();
      }
      else if(Titanium.UI.orientation == Titanium.UI.LANDSCAPE_LEFT 
          || Titanium.UI.orientation == Titanium.UI.LANDSCAPE_RIGHT){
          app.config.rotateLandscape();
    
      }
      else{
    
      }
    
    — answered August 5th 2010 by Christian Zacharias
    permalink
    0 Comments
  • If you use titanium 3.x try Titanium.Gesture.orientation

    Titanium.Gesture.orientation : NumberREADONLY
    Orientation of the current window.

    http://docs.appcelerator.com/titanium/3.0/#!/api/Titanium.Gesture

    — answered September 11th 2014 by Wu Baiquan
    permalink
    0 Comments
  • Here is simple one to check the orientation.

    // detect orientation
    var orientation = function () {
        var orientation = 'UNKNOWN';
        switch (Ti.UI.orientation) {
            case Ti.UI.PORTRAIT:
            case Ti.UI.UPSIDE_PORTRAIT:
                orientation = 'PORTRAIT';
                break;
            case Titanium.UI.LANDSCAPE_LEFT:
            case Titanium.UI.LANDSCAPE_RIGHT:
                orientation = 'LANDSCAPE';
                break;
        }
        return orientation;
    };
    

    Just call orientation wherever you need it. Example to display on info Log.

    Ti.API.info(orientation);
    

    Hope this is helpful. Thanks!

    — answered May 14th 2015 by Madan Sapkota
    permalink
    0 Comments
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.