Avg. Rating 3.6

Problem

You just need to center AIR window on screen both vertically and horizontally. How do I center my AIR window?

Solution

Use screen resolution and native window size properties to set desired coordinates.

Detailed explanation

Calculate and set AIR window coordinates

Set AIR window x coordinate using Capabilities.screenResolutionX and nativeWindow.width. Do the same for y coordinate with Capabilities.screenResolutionY and nativeWindow.height. Use the simple formula:

// Align native AIR application window horizontally and vertically
nativeWindow.x = (Capabilities.screenResolutionX - nativeWindow.width) / 2;
nativeWindow.y = (Capabilities.screenResolutionY - nativeWindow.height) / 2;

The code is simple, but I hope it will save time to someone. To quick test it in Flex, paste the code below in new MXML application:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
    width="450" height="400" layout="absolute"
    creationComplete="creationCompleteHandler(event);">

    <mx:Script>
        <![CDATA[
            private function creationCompleteHandler(event: Event): void
            {
                nativeWindow.x = (Capabilities.screenResolutionX - nativeWindow.width) / 2;
                nativeWindow.y = (Capabilities.screenResolutionY - nativeWindow.height) / 2;
            }
        ]]>
    </mx:Script>
</mx:WindowedApplication>

 


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes