Tuesday, 7 August 2012

Auto orientation in AIR Android

Auto orientation is a feature in AIR Android that changes the orientation of the screen when the device is physically rotated. To use this feature you need to firstly check the ‘Auto orientation’ checkbox in the Android settings. Alternatively, you can set the ‘autoOrients’ tag to true in the application descriptor file.


The ‘StageOrientationEvent’ is used to detect the screen rotations. There are two events that can get dispatched: StageOrientationEvent.ORIENTATION_CHANGE and StageOrientationEvent.ORIENTATION_CHANGING. The former dispatches the event when the screen rotation has changed and the later dispatches the event during the screen rotation. These events have two properties: afterOrientation and beforeOrientation which provide a string of the orientation either the before or after. Here is an example that displays a message based on the rotation of the screen.

//This ensures the orientation of the screen will work properly.
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

//Add text field on the stage.
var tf:TextField = new TextField();
tf.text = 'Rotate the screen';

//Listener to detect screen rotate.
stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, changeHandler);

//This function uses the afterOrientation property to listen for events 
//after the screen has changed. A diffent message is displayed depending 
//on the rotation of the screen.
function changeHandler(e:StageOrientationEvent):void
  if(stage.orientation == StageOrientation.DEFAULT)
   tf.text = 'Potrait';
  else if(stage.orientation == StageOrientation.ROTATED_RIGHT ||
    stage.orientation == StageOrientation.ROTATED_LEFT)
   tf.text = 'Landscape';


  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP