03
Jun
08

Changing the text alignment of a List control in Flex

The following example shows how you can change the text alignment in a Flex List control by setting the textAlign style.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/06/03/changing-the-text-alignment-of-a-list-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Array id="arr">
        <mx:Object label="One" />
        <mx:Object label="Two" />
        <mx:Object label="Three" />
        <mx:Object label="Four" />
        <mx:Object label="Five" />
        <mx:Object label="Six" />
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="textAlign:">
                <mx:ComboBox id="comboBox"
                        dataProvider="[left,center,right]" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:List id="list"
            dataProvider="{arr}"
            textAlign="{comboBox.selectedItem}"
            width="100" />

</mx:Application>

View source is enabled in the following example.

You can also set the textAlign style 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/06/03/changing-the-text-alignment-of-a-list-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Style>
        List {
            textAlign: center;
        }
    </mx:Style>

    <mx:Array id="arr">
        <mx:Object label="One" />
        <mx:Object label="Two" />
        <mx:Object label="Three" />
        <mx:Object label="Four" />
        <mx:Object label="Five" />
        <mx:Object label="Six" />
    </mx:Array>

    <mx:List id="list"
            dataProvider="{arr}"
            width="100" />

</mx:Application>

Or, you can set the textAlign style using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/06/03/changing-the-text-alignment-of-a-list-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            private function comboBox_change(evt:Event):void {
                list.setStyle("textAlign", comboBox.selectedItem);
            }
        ]]>
    </mx:Script>

    <mx:Array id="arr">
        <mx:Object label="One" />
        <mx:Object label="Two" />
        <mx:Object label="Three" />
        <mx:Object label="Four" />
        <mx:Object label="Five" />
        <mx:Object label="Six" />
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="textAlign:">
                <mx:ComboBox id="comboBox"
                        dataProvider="[left,center,right]"
                        change="comboBox_change(event);" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:List id="list"
            dataProvider="{arr}"
            width="100" />

</mx:Application>

2 Responses to “Changing the text alignment of a List control in Flex”


  1. 1 Sacen Dec 17th, 2008 at 7:40 pm

    Hi,
    Thanks for the above example, I got a question how do you add a discription section to each image? urgent needs it for my project.

    Thanks

    Sacen

  2. 2 daniel Feb 26th, 2009 at 2:42 pm

    hi Peter, nice example, what about if i’ve an icon on each item, and i want to align it to the rigth, just after the text of the item,

    i tried this example, but my icon keep align to the left, i think i will have to use item rendered stuff; will be this neccesary?

    greets!
    Daniel

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;".




Badge Farm

  • Powered by Redoable 1.2
  • Cornify
  • Feeds burnt by Feedburner
  • Feed