Titanium Community Questions & Answer Archive

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

orientationchange is never fired

Hello,

I'm trying to implement detection of orientation change in my Android app, to change the layout of elements on the screen.

My application is made by a single window, with some labels and text inputs inside a ScrollView. I implemented this listener:

Ti.Gesture.addEventListener('orientationchange',function(e){
  alert('Orientation changed!')
});

But it's never fired.

I can't understand the reason, also because it's working in the kitchensink app, and I don't find antything different in my approach. Can someone help me?

I'm developing on Windows, with Titanium 1.4.

Thank you in advance.

— asked October 30th 2010 by Riccardo Raneri
  • android
  • orientation
  • orientationchange
0 Comments

4 Answers

  • Accepted Answer

    in fact it looks like Ti.Gesture.addEventListener in app.js doesn't work

    you should consider having a separate file for your win1 (which is in my opinion a good pratice)

    app.js :

    Titanium.UI.setBackgroundColor('#000');
    var tabGroup = Titanium.UI.createTabGroup();
    var win1 = Titanium.UI.createWindow({   title:'Tab 2',   backgroundColor:'#fff', url:'test.js'});
    var tab1 = Titanium.UI.createTab({  icon:'KS_nav_views.png',   title:'Tab 1',   window:win1});
    
    var win2 = Titanium.UI.createWindow({   title:'Tab 2',   backgroundColor:'#fff'});
    var tab2 = Titanium.UI.createTab({   icon:'KS_nav_ui.png',   title:'Tab 2',   window:win2});
    
    tabGroup.addTab(tab1);
    tabGroup.addTab(tab2);
    
    tabGroup.open();
    

    test.js

    Ti.Gesture.addEventListener('orientationchange',function(e){
     alert('Orientation changed!')
    });
    
    — answered October 31st 2010 by Stephane Pelamourgues
    permalink
    0 Comments
  • i've tried your code (mac) and changing orientation (numpad 7) show the alert.

    can you provide your whole code so we can investigate a bit more ?

    — answered October 30th 2010 by Stephane Pelamourgues
    permalink
    0 Comments
  • Hi Stephane,

    Thank you very much for you answer.
    The whole code is a bit long, anyway I tried also adding the listener to the standard "Hello World" app that is generated automatically when a new project is created in Titanium, and it's not working both on emulator (APIs 1.6) and on my Motorola Milestone (Android Froyo preview). Here's the full code:

    // this sets the background color of the master UIView (when there are no windows/tab groups on it)
    Titanium.UI.setBackgroundColor('#000');
    
    // create tab group
    var tabGroup = Titanium.UI.createTabGroup();
    
    
    //
    // create base UI tab and root window
    //
    var win1 = Titanium.UI.createWindow({  
        title:'Tab 1',
        backgroundColor:'#fff'
    });
    var tab1 = Titanium.UI.createTab({  
        icon:'KS_nav_views.png',
        title:'Tab 1',
        window:win1
    });
    
    var label1 = Titanium.UI.createLabel({
        color:'#999',
        text:'I am Window 1',
        font:{fontSize:20,fontFamily:'Helvetica Neue'},
        textAlign:'center',
        width:'auto'
    });
    
    win1.add(label1);
    
    //
    // create controls tab and root window
    //
    var win2 = Titanium.UI.createWindow({  
        title:'Tab 2',
        backgroundColor:'#fff'
    });
    var tab2 = Titanium.UI.createTab({  
        icon:'KS_nav_ui.png',
        title:'Tab 2',
        window:win2
    });
    
    var label2 = Titanium.UI.createLabel({
        color:'#999',
        text:'I am Window 2',
        font:{fontSize:20,fontFamily:'Helvetica Neue'},
        textAlign:'center',
        width:'auto'
    });
    
    win2.add(label2);
    
    
    
    //
    //  add tabs
    //
    tabGroup.addTab(tab1);  
    tabGroup.addTab(tab2);  
    
    
    // open tab group
    tabGroup.open();
    
    Ti.Gesture.addEventListener('orientationchange',function(e){
      alert('Orientation changed!')
    });
    
    — answered October 30th 2010 by Riccardo Raneri
    permalink
    0 Comments
  • Please check this QA and see if it helps.

    http://developer.appcelerator.com/question/71051/android-monitor-orientation

    — answered October 30th 2010 by John McKnight
    permalink
    2 Comments
    • Hello,

      No, unfortunately it doesn't work. I tried to add navBarHidden: false to the Titanium.UI.createWindow statement but I'm still not receiving the alert on orientation change.

      — commented October 31st 2010 by Riccardo Raneri
    • Does the blog post help? http://blog-de-satta.blogspot.com/2010/08/orientation-changes.html

      — commented October 31st 2010 by John McKnight
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.