Styling Alert controls in Flex using the StyleManager class and setStyle() methods

by Peter deHaan on February 29, 2008

in Alert, CSSStyleDeclaration, StyleManager

The following example shows how you can style a Flex Alert control using the static StyleManager.getStyleDeclaration() method, and the setStyle() method.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/02/29/styling-alert-controls-in-flex-using-the-stylemanager-class-and-setstyle-methods/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();">

    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.styles.StyleManager;

            private var alert:Alert;
            private var alertCSS:CSSStyleDeclaration;

            private function init():void {
                alertCSS = StyleManager.getStyleDeclaration("Alert");
            }

            private function showAlert(color:Object):void {
                alertCSS.setStyle("modalTransparencyColor", color);
                alertCSS.setStyle("themeColor", color);
                alert = Alert.show("The quick brown fox...");
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Button label="Red"
                themeColor="red"
                click="showAlert('red');" />
        <mx:Button label="Orange"
                themeColor="haloOrange"
                click="showAlert('haloOrange');" />
        <mx:Button label="Yellow"
                themeColor="yellow"
                click="showAlert('yellow');" />
        <mx:Button label="Green"
                themeColor="haloGreen"
                click="showAlert('haloGreen');" />
        <mx:Button label="Blue"
                themeColor="haloBlue"
                click="showAlert('haloBlue');" />
    </mx:ApplicationControlBar>

</mx:Application>

View source is enabled in the following example.

{ 3 comments… read them below or add one }

1 Marco Antonio March 10, 2009 at 9:05 am

So cool!

Great idea and great code.

Thank’s!

[ ]’s
Marco Antonio
Embu/SP/Brazil

Reply

2 nanu November 24, 2009 at 10:37 am

If you are setting background images using truly dynamic runtime CSS capabilities as shown below (as opposed to compiling with mxmlc) this method does not seem to work!

alertCSS.setStyle(“backgroundImage”, “../images/myImage1.png”); // does not work

On the other had compiling a swf with the following selector works correctly.

.headertopcontent {
backgroundImage: ‘../images/myimages.png’;
color: #202020;
}

Are there any restrictions on what properties the alertCSS object can set? It looks like it can change colors but not manipulate properties that deal with images/urls… Assuming of course the object that is being styled dynamically can handle those properties…

Reply

3 Peter deHaan November 24, 2009 at 1:14 pm

@nanu,

Can you please file a bug at http://bugs.adobe.com/flex/ and include a simple test case.

Thanks,
Peter

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: