Horizontally aligning items in a FormItem container in Flex

by Peter deHaan on March 18, 2008

in FormItem, Forms

The following example shows how you can align form items horizontally by setting the horizontalAlign style to “left”, “center”, or “right” on the Flex FormItem container.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/18/horizontally-aligning-form-items-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Style>
        Form {
            backgroundColor: white;
            backgroundAlpha: 0.2;
            borderColor: black;
            borderStyle: solid;
            borderThickness: 1;
        }

        FormItem {
            labelStyleName: myFormItemLabelStyleName;
        }

        .myFormItemLabelStyleName {
            fontWeight: bold;
        }
    </mx:Style>

    <mx:Form id="form" width="300">
        <mx:FormItem label="left:"
                horizontalAlign="left"
                width="100%">
            <mx:Image source="@Embed('assets/air_appicon-tn.gif')" />
        </mx:FormItem>
        <mx:FormItem label="center:"
                horizontalAlign="center"
                width="100%">
            <mx:Image source="@Embed('assets/fl_appicon-tn.gif')" />
        </mx:FormItem>
        <mx:FormItem label="right:"
                horizontalAlign="right"
                width="100%">
            <mx:Image source="@Embed('assets/fx_appicon-tn.gif')" />
        </mx:FormItem>
    </mx:Form>

</mx:Application>

View source is enabled in the following example.

{ 6 comments… read them below or add one }

1 Raul Riera March 19, 2008 at 6:57 am

Have you tried working with the Schedule Framework of flexlib? some examples of that would really be helpful :) (I know I shouldnt be asking, you are doing a great job)

Reply

2 peterd March 19, 2008 at 10:34 am

Raul,

I haven’t looked at it yet. I’m still busy going through the core Flex/Player API and finding lots of interesting things. Maybe in the future I’ll expand out and do some SWFObject posts and stuff from FlexLib and the Yahoo! Maps Flex API.

Peter

Reply

3 Christian May 9, 2008 at 12:03 pm

I’m interested in aligning the LABELS to the left or above the form field. Have you found any solutions for this?

Reply

4 peterd May 9, 2008 at 4:19 pm

Christian,

I haven’t found a solution for that. The workaround is pretty simple as you can manually add a Label control within the FormItem container.

Feel free to log a bug at http://bugs.adobe.com/flex/ (if you file a bug and post the bug number here and I’ll vote for it). You can also ask on the FlexCoders list and see if anybody has a solution (you may be able to extend the FormItem container and change the default layout).

Peter

Reply

5 Francisco Villa Jr. October 9, 2008 at 3:11 pm

This is how you align the formitem labels to the left.

Set the formitem’s “labelStyleName” attribute to some style such as “myFormLabelStyle”. Then, define the style as such:

.myFormLabelStyle
{
	textAlign: left;
}

Reply

6 peterd October 9, 2008 at 3:33 pm

Francisco Villa Jr.,

Good tip! I have a post on that at “Changing the label text alignment in a FormItem container in Flex”.

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: