11
Aug
07

Styling the Flex Alert control

This is a more complex version of my previous example. This time in addition to making the Alert control’s text non-selectable, we use an embedded font for the Alert title, message, buttons, as well as edit the drop shadow, remove the rounded corners, and remove the button skins.

Full code after the jump.

Since this example uses the mx_internal namespace, you can’t always depend on this behavior to work in future versions of the Flex SDK. Use at your own risk.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/11/styling-the-flex-alert-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="showAlert()"
        applicationComplete="init()">

    <!-- Used by the ApplicationControlBar control. -->
    <mx:String id="fileName" />
    <mx:String id="fileSize" />

    <!-- Used by the Alert control. -->
    <mx:String id="message">The quick brown fox jumped over the lazy dog.

The quick brown fox jumped over the lazy dog.</mx:String>
    <mx:String id="title">The quick brown fox jumped over the lazy dog?</mx:String>

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

            private var a:Alert;

            private function init():void {
                var appInfo:LoaderInfo = Application.application.loaderInfo;
                /* Just grab the filename from the SWF URL. */
                fileName = (appInfo.url).split("/").pop();
                /* Convert bytes to kilobytes. */
                fileSize = (appInfo.bytesTotal / 1024).toFixed(2);
            }

            private function showAlert():void {
                Alert.yesLabel = "Accept";
                Alert.noLabel = "Reject";
                Alert.buttonWidth = 120;

                a = Alert.show(
                        message,
                        title,
                        Alert.NO | Alert.YES
                    );
                /* Make the Alert form's text non-selectable. */
                a.mx_internal::alertForm.mx_internal::textField.selectable = false;
            }
        ]]>
    </mx:Script>

    <mx:Style>
        @font-face{
            src: url("./fonts/base02.ttf");
            fontFamily: "Base02";
        }

        Alert {
            titleStyleName: "alertTitle";
            messageStyleName: "alertMessage";
            buttonStyleName: "alertButton";
            dropShadowEnabled: true;
            shadowDistance: 5;
            shadowDirection: right;
            cornerRadius: 0;
            embedFonts: true;
            fontFamily: Base02;
        }

        .alertTitle {
            letterSpacing: 0;
            fontSize: 14;
            color: red;
        }

        .alertMessage {
            letterSpacing: 0;
            fontSize: 10;
            fontWeight: normal;
            color: black;
        }

        .alertButton {
            letterSpacing: 0;
            fontSize: 11;
            cornerRadius: 10;
            fontWeight: normal;
            textRollOverColor: white;
            color: red;
            skin: ClassReference(null);
        }
    </mx:Style>

    <!-- Display SWF name and file size. -->
    <mx:ApplicationControlBar id="applicationControlBar" dock="true">
        <mx:Label id="info" text="{fileName} ({fileSize}kb)" />
    </mx:ApplicationControlBar>

    <!-- Click to launch Alert control. -->
    <mx:Button label="Launch Alert" click="showAlert()" />

</mx:Application>

View source is enabled in the following application.

I didn’t include the TrueType font in the source code ZIP. If you want to grab that exact font, head over to the creator’s site @ http://www.stereo-type.net/ and download “Base 02″.


9 Responses to “Styling the Flex Alert control”


  1. 1 nl Sep 11th, 2007 at 10:01 am

    I tried compiling this example and it does work fine except for the button face which still have the rounded borders.

    The same thing happened when i tried another example from this site.

    i’m not sure, but i think it’s the line

    skin: ClassReference(null);

    that does not work.

    any idea?

    I’m using the flex sdk 2.0.1 with hotfix 1.

    I’d like to be able to use the button class without the standard face so i’d really like this to work.

  2. 2 peterd Sep 11th, 2007 at 10:17 am

    nl,

    I believe the “skin” style is new to Flex 3.

    http://livedocs.adobe.com/flex/201/langref/mx/controls/Button.html#styleSummary (Flex 2.0.1)
    http://livedocs.adobe.com/labs/flex3/langref/mx/controls/Button.html#styleSummary (Flex 3 beta)

    In Flex 2 you may need to set the various skin states (disabledSkin, downSkin, overSkin, upSkin, and the various selected*Skin styles) separately.

    Sorry for the confusion, I primarily do all these entries in Flex 3 and don’t do much backwards compatibility testing (as you probably discovered).

    Peter

  3. 3 nl Sep 12th, 2007 at 1:54 am

    ok!

    That makes sense.
    Thanks for the info.

  4. 4 nocturnalfrog Jan 1st, 2008 at 2:24 pm

    Hi, I was wondering if there is a way to change the blur en transparant white effect of the Alert class…

  5. 5 peterd Jan 1st, 2008 at 7:57 pm

    nocturnalfrog,

    Check out the following styles: modalTransparencyBlur, modalTransparency, modalTransparencyColor, and modalTransparencyDuration.

    For more information, and an example, see “Changing a modal Alert control’s blur, transparency and transparency color”.

    Hope that helps,
    Peter

  6. 6 Philip Apr 11th, 2008 at 10:51 am

    How do you switch the yesButton/noButton actions ?

  7. 7 Philip Apr 11th, 2008 at 11:00 am

    I solved as following:

    a.defaultButton.addEventListener(MouseEvent.CLICK, setClick);
    private function setClick(evt:MouseEvent):void
    {
        // my actions...
    }
    
  8. 8 Ali Tan Ucer Apr 27th, 2008 at 7:22 am

    How and why we can not be able to resize the Alert popup.
    It is set to a certain size and i still couldnt find a way to resize it.
    If you use large typeface everything is overlaping. It is just really anoying.

  9. 9 peterd Apr 27th, 2008 at 5:27 pm

    Ali Tan Ucer,

    Does something like this work for you?

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    
        <mx:Script>
            <![CDATA[
                import mx.managers.PopUpManager;
                import mx.controls.Alert;
    
                private var alert:Alert;
    
                private function init():void {
                    alert = new Alert();
                    alert.width = 500;
                    alert.height = 400;
                    alert.text = lorem;
                    PopUpManager.addPopUp(alert, this);
                    PopUpManager.centerPopUp(alert);
                }
            ]]>
        </mx:Script>
    
        <mx:String id="lorem" source="lorem.html" />
    
        <mx:Button click="init();" />
    
    </mx:Application>
    

    Peter

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".