Home > Flex 4 > Adding a background color to a spark Group

Adding a background color to a spark Group

In halo you could add a background color to any container by setting the backgroundColor style, but this is not the case for every spark container. Setting this style on components that extend from SkinnableContainer will work fine, but this style doesn’t exist on the Group component.

The reason for this is the Group container is very lightweight and has no skin and thus no visuals associated with it. It’s role is to layout its children which are visual elements.

You can add a background color to a Group by using a Rect with 100% width and height as the first element:

<s:Group>
    <s:Rect width="100%" height="100%">
        <s:fill><s:SolidColor color="0xFF0000" /></s:fill>
    </s:Rect>
 
    <s:Label text="group content..." />
 
</s:Group>

This works great with the default BasicLayout, but is a little trickier with a VerticalLayout or HorizontalLayout since that first element will be moved around by the layout.

Here is an example of how to add a background color to a VGroup (which is a Group with a VerticalLayout):

<s:Group>
    <s:Rect width="100%" height="100%">
        <s:fill><s:SolidColor color="0xFF0000" /></s:fill>
    </s:Rect>
 
    <s:VGroup>
        <s:Label text="group content..." />
        <s:Label text="group content..." />
        <s:Label text="group content..." />
    </s:VGroup>
 
</s:Group>

Same Idea for HGroup (Group with a HorizontalLayout):

<s:Group>
    <s:Rect width="100%" height="100%">
        <s:fill><s:SolidColor color="0xFF0000" /></s:fill>
    </s:Rect>
 
    <s:HGroup>
        <s:Label text="group content..." />
        <s:Label text="group content..." />
        <s:Label text="group content..." />
    </s:HGroup>
 
</s:Group>

And TileGroup (Group with a TileLayout):

<s:Group>
    <s:Rect width="100%" height="100%">
        <s:fill><s:SolidColor color="0xFF0000" /></s:fill>
    </s:Rect>
 
    <s:TileGroup requestedColumnCount="2">
        <s:Label text="group content..." />
        <s:Label text="group content..." />
        <s:Label text="group content..." />
        <s:Label text="group content..." />
    </s:TileGroup>
 
</s:Group>

Note: This sample requires Flex SDK 4.0.0.12093 or higher. You can get the latest SDK builds from opensource.adobe.com.

  1. December 23rd, 2009 at 05:48 | #1

    How about using

  2. March 1st, 2010 at 22:21 | #2

    Ah, that explains it! I was wondering how to get rid of that gray-green color. And why there was no background color property. Makes perfect sense now.

    Thanks,
    David Salahi

  3. felix
    June 1st, 2010 at 18:24 | #3

    Or use a ‘BorderContainer’ instead of a group and specify the ‘backgroundColor’.

  4. Vamsi
    September 13th, 2010 at 22:59 | #4

    Its better to use this way, as bordercontaienr is bit heavy when compared to group + Rect

  5. increpare
    February 23rd, 2011 at 10:39 | #5

    thanks for this
    <3

  6. May 5th, 2011 at 04:46 | #6

    Hi,

    How can this be done from AS. i have created a vgroup in AS and i want to set background to it. kindly help in adding background image for the same.

    Regards,
    Prakash

  7. Steven Shongrunden
    May 30th, 2011 at 19:20 | #7

    @prakash – Here is an example in ActionScript:

    import mx.graphics.SolidColor;
    import spark.components.Group;
    import spark.primitives.Rect;

    public function addGroup():void {
    var g:Group = new Group();
    var bg:Rect = new Rect();
    bg.fill = new SolidColor(0xFF0000);
    bg.left = 0;
    bg.right = 0;
    bg.top = 0;
    bg.bottom = 0;
    g.addElement(bg);
    addElement(g);

    // now add your elements to the Group
    g.addElement(other element);
    }

  8. anon
    June 10th, 2011 at 00:21 | #8

    thx!

  9. September 8th, 2011 at 15:01 | #9

    I thought I was going crazy for a minute there… So easy a caveman could do it.

    THANKS!

  10. September 9th, 2011 at 05:57 | #10

    Another generalized takeaway from this- when you are looking for something in spark classes … it just may not be there yet. Check mx.

    Again, Great post.

  11. subash
    November 3rd, 2011 at 12:21 | #11

    Thank you very much. This stuff safed my life :)

  12. December 20th, 2011 at 08:07 | #12

    Thanks. Brand new to Flash Builder and still getting used to layouts. This helped a lot.

  1. No trackbacks yet.