Using the FxAnimateColor effect to animate the content background color of a FxTextInput control in Flex Gumbo

by Peter deHaan on March 6, 2009

in FxAnimateColor, FxTextInput, Validator, beta

The following example shows how you can use the FxAnimateColor effect to animate the background color on a Flex Gumbo FxTextInput control.

Full code after the jump.

To use the following code, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3. For more information on downloading and installing the Gumbo SDK into Flex Builder 3, see “Using the beta Gumbo SDK in Flex Builder 3″.

View MXML

<?xml version="1.0"?>
<!-- http://blog.flexexamples.com/2009/03/06/using-the-fxanimatecolor-effect-to-animate-the-content-background-color-of-a-fxtextinput-control-in-flex-gumbo/ -->
<FxApplication name="FxTextInput_contentBackgroundColor_FxAnimateColor_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        backgroundColor="white">
    <layout>
        <BasicLayout />
    </layout>

    <Script>
        <![CDATA[
            private function textInput_focusIn(evt:FocusEvent):void {
                colorEffect.play([evt.currentTarget]);
            }

            private function textInput_focusOut(evt:FocusEvent):void {
                colorEffect.stop();
                evt.currentTarget.setStyle("contentBackgroundColor", "white");
            }
        ]]>
    </Script>

    <Declarations>
        <FxAnimateColor id="colorEffect"
                colorPropertyName="contentBackgroundColor"
                colorFrom="0xFFFFFF"
                colorTo="0xFF0000"
                repeatBehavior="reverse"
                repeatCount="0"
                duration="500" />
    </Declarations>

    <VGroup horizontalCenter="0" verticalCenter="0">
        <FxTextInput id="textArea1"
                text="The quick brown fox jumps over the lazy dog."
                focusIn="textInput_focusIn(event);"
                focusOut="textInput_focusOut(event);" />
        <FxTextInput id="textArea2"
                text="The quick brown fox jumps over the lazy dog."
                focusIn="textInput_focusIn(event);"
                focusOut="textInput_focusOut(event);" />
        <FxTextInput id="textArea3"
                text="The quick brown fox jumps over the lazy dog."
                focusIn="textInput_focusIn(event);"
                focusOut="textInput_focusOut(event);" />
    </VGroup>

</FxApplication>

You can also animate the content background color on a Flex Gumbo FxTextInput control by using validators, as seen in the following example:

View MXML

<?xml version="1.0"?>
<!-- http://blog.flexexamples.com/2009/03/06/using-the-fxanimatecolor-effect-to-animate-the-content-background-color-of-a-fxtextinput-control-in-flex-gumbo/ -->
<FxApplication name="FxTextInput_contentBackgroundColor_FxAnimateColor_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        backgroundColor="white">
    <layout>
        <BasicLayout />
    </layout>

    <Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.validators.Validator;
            import mx.events.ValidationResultEvent;

            private function validateAll():void {
                colorEffect.stop();
                var results:Array = Validator.validateAll(validatorArr);
                if (results.length == 0) {
                    Alert.show("SUCCESS");
                } else {
                    var targetArr:Array = [];
                    var result:ValidationResultEvent;
                    for each (result in results) {
                        targetArr.push(result.currentTarget.source);
                    }
                    colorEffect.play(targetArr);
                }
            }

            private function textInput_invalid(trgt:Object):void {
                trgt.setStyle("contentBackgroundColor", "red");
            }

            private function textInput_valid(trgt:Object):void {
                trgt.setStyle("contentBackgroundColor", "white");
            }
        ]]>
    </Script>

    <Declarations>
        <FxAnimateColor id="colorEffect"
                colorPropertyName="contentBackgroundColor"
                colorFrom="0xFFFFFF"
                colorTo="0xFF0000"
                repeatBehavior="reverse"
                repeatCount="0"
                duration="500" />

        <Array id="validatorArr">
            <StringValidator id="validator1"
                    source="{textInput1}"
                    property="text"
                    tooShortError="This string is shorter than the minimum allowed length of 4."
                    tooLongError="This string is longer than the maximum allowed length of 20."
                    minLength="4"
                    maxLength="20"
                    valid="textInput_valid(event.currentTarget.source);"
                    invalid="textInput_invalid(event.currentTarget.source);" />
            <StringValidator id="validator2"
                    source="{textInput2}"
                    property="text"
                    tooShortError="This string is shorter than the minimum allowed length of 4."
                    tooLongError="This string is longer than the maximum allowed length of 20."
                    minLength="4"
                    maxLength="20"
                    valid="textInput_valid(event.currentTarget.source);"
                    invalid="textInput_invalid(event.currentTarget.source);" />
        </Array>
    </Declarations>

    <Form id="frm"
            horizontalCenter="0"
            verticalCenter="0">
        <FormItem label="FxTextInput #1:" required="true">
            <FxTextInput id="textInput1" maxChars="20" />
        </FormItem>
        <FormItem label="FxTextInput #2:" required="true">
            <FxTextInput id="textInput2" maxChars="20" />
        </FormItem>
        <FormItem label="FxTextInput #3:">
            <FxTextInput id="textInput3" maxChars="20" />
        </FormItem>
        <FormItem>
            <FxButton id="btn"
                    label="Validate!"
                    click="validateAll();" />
        </FormItem>
    </Form>

</FxApplication>

This entry is based on a beta version of the Flex Gumbo 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 Gumbo SDK.

{ 2 comments… read them below or add one }

1 Joseph March 7, 2009 at 11:50 am

I wonder why when you give very helpful tips in flex 3 you also show the swf file in the post, except for Gumbo posts?

Reply

2 Peter deHaan March 8, 2009 at 9:20 am

Joseph,

The Gumbo posts will all need to be updated after the Fx prefix is removed. So I didn’t want to create/upload a bunch of SWFs and then recreate them all in the future. I’m planning on adding SWFs to these older examples (time permitting) when I update them all later after the big API renaming. For more information on the “Dropping the Fx Prefix” page on the Flex Gumbo page on the opensource.adobe.com website.

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: