Skip to content

[Titanium] How to detect window orientation (portrait vs landscape) at start-up

Titanium provides a simple way to detect window orientation, Titanium.UI.Window.orientation, but it seems to initialize late. In an application I’m developing, I needed to check the window orientation right at start-up, more specifically I used this:
if(Titanium.UI.Window.orientation==Titanium.UI.PORTRAIT) {stuff}
but this didn’t seem to work.

Running a simple
revealed that the variable sadly is undefined at that stage (TBH, I didn’t check if it was defined later since that’s not where I need it).

So I made a custom function, based on window size, which turns out to be defined early enough for me. Here is the function, quite obvious, really:
var isPortrait = function(){
return (Ti.Platform.displayCaps.platformHeight > Ti.Platform.displayCaps.platformWidth);

Then just call it with isPortrait() to get a boolean with value true if the window is oriented in portrait and false if it’s oriented in landscape.

Note that I only needed to make the difference between portrait and landscape, I don’t really have an idea to make the difference between properly oriented and upside-down portrait, or between left of right landscape, as the Titanium.UI.Window.orientation property offers (its possible values are: Titanium.UI.LANDSCAPE_LEFT, Titanium.UI.LANDSCAPE_RIGHT, Titanium.UI.PORTRAIT and Titanium.UI.UPSIDE_PORTRAIT)

Posted in programming.

Tagged with .

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

or, reply to this post via trackback.

Please solve the CAPTCHA below in order to fight spamWordPress CAPTCHA