In a previous example, “Creating full-screen Flex applications”, we saw how to toggle full screen mode in a Flex application by setting the allowFullScreen parameter in the HTML wrapper template’s AC_FL_RunContent() method.
The following example shows how you can enable full screen from your HTML template to your Flex applications using the Flex 4 SDK (which uses SWFObject now).
Full code after the jump.
The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 beta, check out the Adobe Flash Builder 4 page on the Adobe Labs site. To download the latest build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4. For instructions on using the beta Flex 4 SDK in Flex Builder 3, see "Using the beta Flex 4 SDK in Flex Builder 3".
<?xml version="1.0" encoding="utf-8"?> <!-- http://blog.flexexamples.com/2009/03/15/creating-full-screen-flex-applications-in-flex-gumbo-swfobject-edition/ --> <s:Application name="Gumbo_FullScreen_test" xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" applicationComplete="init();"> <fx:Script> <![CDATA[ [Bindable] private var fullScreenState:String; private function init():void { fullScreenState = stage.displayState; } private function btn_click(evt:MouseEvent):void { if (btn.selected) { fullScreenState = StageDisplayState.FULL_SCREEN; } else { fullScreenState = StageDisplayState.NORMAL; } try { stage.displayState = fullScreenState; } catch (any:*) { // ignore } } ]]> </fx:Script> <s:VGroup horizontalCenter="0" top="20" width="150"> <s:SimpleText text="width={width}" /> <s:SimpleText text="height={height}" /> <s:SimpleText text="displayState={fullScreenState}" /> <s:ToggleButton id="btn" label="Toggle fullscreen" click="btn_click(event);" width="100%" /> </s:VGroup> </s:Application>
NOTE: It appears that the /html-template/index.template.html sets the allowfullscreen flag to “true” by default now, so you no longer need to manually edit the .HTML templates.
View source is enabled in the following example.
This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

{ 8 comments… read them below or add one }
Hi Peter. Me again! I’m trying to do this today and am finding that I can’t set a listener up on the stage object. Has this changed in the latest SDK?
@lee,
I just updated the example to work with the latest API (or 4.0.0.8903+, at least) and it worked fine. Which error(s) were you seeing with the example?
Peter
Added SWF.
@lee,
What about this?
Peter
Peter,
In your example, if the escape key is used to return to normal from fullscreen, the togglebutton remains in a selected state, and the text for displaystate continues to show “fullscreen”.
I’m currently trying to use a toggle button, but want it to return to its unselected state if the user hits the escape key to return to normal view.
The following (executed during creationComplete of a custom button) results in an “Error #1009: Cannot access a property or method of a null object reference.” on “stage”:
stage.addEventListener( FullScreenEvent.FULL_SCREEN, fullScreenHandler, false, 0, true );
or, alternately:
FlexGlobals.topLevelApplication.stage.addEventListener( FullScreenEvent.FULL_SCREEN, fullScreenHandler, false, 0, true );Thoughts on how to resolve this?
@Matt S.,
This works for me in Flex 4.0.0.9027:
Peter
Thanks, Peter.
That solution definitely works in a situation where the components and handlers are defined directly in the Application object, and the stage listener can be handled on applicationComplete.
For the benefit of those seeking a solution similar to my case…
In my case (handling this in a custom button component), setting an Event.ADDED_TO_STAGE handler was the solution. This allowed me to set the FullScreenEvent.FULL_SCREEN listener on stage in the custom component; since stage = null until the component is added stage’s display list.
(Noting that I’m presently using Halo, not Spark, components with Flex 4 [Gumbo])
Sorry, if you’re posting XML, MXML, or HTML, you’ll need to escape your < chars as < and your > chars as >
I’ll try and update the comments form to mention that.
Peter