Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simply handle orientation changes?

I'm not talking about doing anything fancy. I'd just like the standard windows and views to rotate when the user rotates the device.

like image 729
Dogweather Avatar asked Mar 16 '11 08:03

Dogweather


People also ask

How can we cope with screen orientation changes?

If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes. You can declare multiple configuration values in the attribute by separating them with a pipe | character.

What issues need to be handled if the orientation of a device is changed at the runtime?

If orientation changes is not handle properly then it results unexpected behavior of the application. When such changes occurs, Android restarts the running Activity means it destroy and again created.

What happens when orientation changes Android?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them . Android does this so that your application can reload resources based on the new configuration.


2 Answers

You need to tell the window which orientations it should support:

var window = Ti.UI.createWindow({
    orientationModes: [
        Ti.UI.LANDSCAPE_LEFT,
        Ti.UI.LANDSCAPE_RIGHT,
        Ti.UI.PORTRAIT,
        Ti.UI.UPSIDE_PORTRAIT
    ]
});

window.open();

You can then listen on the orientation changes with a listener like so:

Ti.Gesture.addEventListener('orientationchange', function(e) {
    Titanium.API.info('Orientation changed');
});

Edit: I think (though I've never tried it) you can also set this in tiapp.xml, which has the added benefit of applying to all windows automatically.

<orientations device="iphone">
    <orientation>Ti.UI.LANDSCAPE_LEFT</orientation>
    <orientation>Ti.UI.LANDSCAPE_RIGHT</orientation>
    <orientation>Ti.UI.PORTRAIT</orientation>
    <orientation>Ti.UI.UPSIDE_PORTRAIT</orientation>
</orientations>
like image 53
Craig Avatar answered Oct 02 '22 20:10

Craig


Titanium.Gesture.addEventListener('orientationchange', function(e) {
    Titanium.API.info('Gesture Change Detected');
    Ti.App.fireEvent('orientationchange', {eventObject:e});
});
like image 41
Aaron Saunders Avatar answered Oct 02 '22 18:10

Aaron Saunders