Applying an effect when an HBox container is resized in Flex

by Peter deHaan on October 1, 2008

in Effects, HBox

The following example shows how you can apply a resize effect to a Flex HBox container by setting the resizeEffect style/effect.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/01/applying-an-effect-when-an-hbox-container-is-resized-in-flex/ -->
<mx:Application name="HBox_resizeEffect_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:HDividedBox id="hDividedBox"
            width="100%"
            height="100%">
        <mx:HBox id="hBox1"
                backgroundColor="haloGreen"
                resizeEffect="Resize"
                width="100%"
                height="100%" />
        <mx:HBox id="hBox2"
                backgroundColor="haloBlue"
                resizeEffect="Resize"
                width="100%"
                height="100%" />
    </mx:HDividedBox>

</mx:Application>

View source is enabled in the following example.

You can also set the resizeEffect style/effect in an external .CSS file or <mx:Style /> block, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/01/applying-an-effect-when-an-hbox-container-is-resized-in-flex/ -->
<mx:Application name="HBox_resizeEffect_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Style>
        HBox {
            resizeEffect: ClassReference("mx.effects.Resize");
        }
    </mx:Style>

    <mx:HDividedBox id="hDividedBox"
            width="100%"
            height="100%">
        <mx:HBox id="hBox1"
                backgroundColor="haloGreen"
                width="100%"
                height="100%" />
        <mx:HBox id="hBox2"
                backgroundColor="haloBlue"
                width="100%"
                height="100%" />
    </mx:HDividedBox>

</mx:Application>

Or, you can set the resizeEffect style/effect using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/01/applying-an-effect-when-an-hbox-container-is-resized-in-flex/ -->
<mx:Application name="HBox_resizeEffect_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <mx:Script>
        <![CDATA[
            import mx.effects.Resize;

            private function init():void {
                hBox1.setStyle("resizeEffect", Resize);
                hBox2.setStyle("resizeEffect", Resize);
            }
        ]]>
    </mx:Script>

    <mx:HDividedBox id="hDividedBox"
            width="100%"
            height="100%">
        <mx:HBox id="hBox1"
                backgroundColor="haloGreen"
                width="100%"
                height="100%" />
        <mx:HBox id="hBox2"
                backgroundColor="haloBlue"
                width="100%"
                height="100%" />
    </mx:HDividedBox>

</mx:Application>

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/01/applying-an-effect-when-an-hbox-container-is-resized-in-flex/ -->
<mx:Application name="HBox_resizeEffect_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <mx:Script>
        <![CDATA[
            import mx.containers.HBox;
            import mx.containers.HDividedBox;
            import mx.effects.Resize;

            private var hBox1:HBox;
            private var hBox2:HBox;
            private var hDividedBox:HDividedBox;

            private function init():void {
                hBox1 = new HBox();
                hBox1.percentWidth = 100;
                hBox1.percentHeight = 100;
                hBox1.setStyle("backgroundColor", "haloGreen");
                hBox1.setStyle("resizeEffect", Resize);

                hBox2 = new HBox();
                hBox2.percentWidth = 100;
                hBox2.percentHeight = 100;
                hBox2.setStyle("backgroundColor", "haloBlue");
                hBox2.setStyle("resizeEffect", Resize);

                hDividedBox = new HDividedBox();
                hDividedBox.percentWidth = 100;
                hDividedBox.percentHeight = 100;
                hDividedBox.addChild(hBox1);
                hDividedBox.addChild(hBox2);
                addChild(hDividedBox);
            }
        ]]>
    </mx:Script>

</mx:Application>

{ 10 comments… read them below or add one }

1 GG October 2, 2008 at 12:47 am

Very very very good thanks a lot

Reply

2 Georges Jentgen October 2, 2008 at 10:43 am

Awesome!!! Like it, will use it :)

Reply

3 mg October 12, 2008 at 6:27 am

There’s a bug… Drag the divider to the right border and release it. First it moves as supposed…

Reply

4 peterd October 12, 2008 at 7:21 am

mg,

Good catch!
I haven’t tested it, but you may be able to solve the problem by defining a minimum width on each of the HBox containers (I only saw the problem if you were dragging left to right, and the second HBox was 0 pixels), or possibly you could fix the example by setting the resizeEffect style/effect on only one of the two HBox containers.

Peter

Reply

5 moko October 21, 2008 at 8:00 am

defining a minWidth=”1″ on the right one “solves” the thing, but why the hell is it happening when you drag left to right border and not when you drag right to left border???

Reply

6 AV November 25, 2008 at 2:26 am

which is the easy way to set the duration, during using this setStyle.

Reply

7 Anonymous March 6, 2009 at 9:43 pm

really cool will use it for sure

Reply

8 Daniel Dourado November 9, 2009 at 11:34 am

Can I get the same effect when resizing a collum in a dataGrid?

Reply

9 Peter deHaan November 9, 2009 at 3:09 pm

@Daniel Dourado,

I don’t believe so, as I didn’t see resizeEffect on the DataGridColumn class.

Peter

Reply

10 Anonymous December 28, 2009 at 5:45 am

Hello,

I want to show or hide left box on clicking of divider. And while hiding on showing i want to use resize effect.

I tried but its not working properly… :’(

Here is my code

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: