Setting or clearing the application background image in Flex

by Peter deHaan on March 14, 2008

in Application

The following example shows how you can remove the default gradient background image for a Flex application by setting the backgroundImage style.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/14/setting-or-clearing-the-application-background-image-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        applicationComplete="init();">

    <mx:Script>
        <![CDATA[
            import mx.events.ItemClickEvent;

            [Bindable]
            private var defaultBackgroundImage:Class;

            private function init():void {
                defaultBackgroundImage = Application.application.getStyle("backgroundImage");
            }

            private function toggleButtonBar_itemClick(evt:ItemClickEvent):void {
                Application.application.setStyle("backgroundImage", evt.item.data);
            }
        ]]>
    </mx:Script>

    <mx:Array id="arr">
        <mx:Object label="default" data="{defaultBackgroundImage}" />
        <mx:Object label="null" data="{null}" />
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="backgroundImage:">
                <mx:ToggleButtonBar id="toggleButtonBar"
                        dataProvider="{arr}"
                        itemClick="toggleButtonBar_itemClick(event);" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

</mx:Application>

View source is enabled in the following example.

You can also set the backgroundImage style in an external .CSS file, or <mx:Style /> block, as shown in the following snippet:

<mx:Style>
    Application {
        backgroundImage: ClassReference(null);
    }
</mx:Style>

Or, you can set the backgroundImage style in MXML, as shown in the following snippet:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        backgroundImage="{null}">

{ 4 comments… read them below or add one }

1 FlexFlip December 9, 2008 at 7:51 am

I am loading a Flex app into a Flash app (I know, don’t ask). The Flex app is made transparent (see example above). Still the Flex app will take over the mouse if it hovers over the loaded FlexApp.swf. Can this be prevented or is this inherent in the way the Flex framework has been set up?

Reply

2 toby April 19, 2009 at 4:39 pm

I was just looking for something like this.

Thanks

Reply

3 hi June 29, 2009 at 11:29 pm

or you could just add this to your

Reply

4 hi June 29, 2009 at 11:30 pm

previous didnt come out

add this to your application starting tag

paddingBottom=”0″ paddingLeft=”0″ paddingRight=”0″ paddingTop=”0″

and the part you’re trying to hide will be completely gone, no need for the complicated response

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: