Center AIR Window on the screen

You might have noticed that Window or WindowedApplication doesn’t load in the center of your screen. By default, window loads somewhere in the left side on your desktop. I personally think, that this is a bad user experience and you should always center window vertically and horizontally on screen. You can do this by setting window x coordinate to (screen.width – window.width)/2  and y coordinate to (screen.height – window.height)/2.

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
    creationComplete="centerWindow(this)" viewSourceURL="srcview/index.html"> 

    <mx:Script>
        <![CDATA[
            import mx.core.Window;

            private function centerWindow(window:WindowedApplication):void
            {
                var screenBounds:Rectangle = Screen.mainScreen.bounds;
                var nativeWindow:NativeWindow  = window.nativeWindow;

                nativeWindow.x = (screenBounds.width - nativeWindow.width)/2;
                nativeWindow.y = (screenBounds.height - nativeWindow.height)/2;
            }
        ]]>
    </mx:Script> 

</mx:WindowedApplication>

One Comment


  1. Ram Sambamurthy
    October 1, 2009 at 4:10 pm

    Will also need to add the following:

    import flash.geom.Rectangle;
    import flash.display.Screen;
    import flash.display.NativeWindow;

Leave a comment