How can I find the user's screen size in AIR?

Yesterday, someone posted to Apollo Coders to ask if they can find out the user's screen resolution in an AIR application. The answer is yes, you can.

You can find this information out using the flash.system.Capabilities class. You need to look at the screenResolutionX and screenResolutionY properties. These are static properties on the Capabilities class, so you do not need to create an instance.

The poster also asked if he could find the height and width of the current window. This on was even easier to answer, because windows (and most UI controls ) have a height and width in the Flex framework.

I can demonstrate both of these answers with a single code sample. This code sample will size the application window to 60% of the screen width:

<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
    preinitialize="preInitializeHandler()"/>

<mx:Script><![CDATA[

import flash.system.Capabilities;
import mx.controls.Alert

private function preInitializeHandler():void{
// set the width to 60% of the full screen this.height = Capabilities.screenResolutionY * .6;
this.width = Capabilities.screenResolutionX * .6;
this.visible = true;
}

]]></mx:Script>

</mx:WindowedApplication>

This code is very similar to something I used in a Flex application for a client. It runs code in the preinitialize event. That code sets the height and width of the WindowedApplication to 60% of the screenResolution. Then it makes the window visible. ( I set the window to be invisible in the app.xml file )

As a note, this should work in a Flex app too, but I haven't tried it.

I got a busy week this week. Thursday I'm at a a Microsoft even in Hartford. I understand they are giving out free software. Friday I'm down in NYC to do some podcast interviews at Flex Camp Wall Street.

Comments
Nick Schneble's Gravatar In AIR you can also use the flash.display.Screen class to get the screen size. However, the Screen class offers one major advantage over Capabilities - the ability to retrieve the "visible bounds" of the screen, which is:

"The bounds of the area on this screen in which windows will be visible. The visibleBounds of a screen excludes the taskbar (and other docked deskbars) on Windows, and excludes the menu bar and, depending on system settings, the dock on Mac OS X."

Really useful when you want to open new AIR windows at the screen edges without worrying if they are partially obscured by some part of the OS!
# Posted By Nick Schneble | 4/16/08 10:03 PM
_Stef's Gravatar Hi,

more for the screen class, when you want catch the size of your screen, if you have more one screen, this class can deal with, and send you the size of the current screen of your app is.

import flash.display.Screen
private function getCurrentScreen():Screen{
var current:Screen;
var screenArray:Array = Screen.screens;
var screens:Array = Screen.getScreensForRectangle(stage.nativeWindow.bounds);
(screens.length > 0) ? current = screens[0] : current = Screen.mainScreen;
return current;
}
var actualScreen:Screen = getCurrentScreen();
trace (actualScreen.bounds.width +' '+actualScreen.bounds.height);


Your site is very usefull, thanks for the job.
# Posted By _Stef | 4/27/08 3:29 AM
All Content Copyright 2005, 2006, 2007 Jeffry Houser. May not be reused without permission
BlogCFC was created by Raymond Camden. This blog is running version 5.8.