Setting the bar color on the ProgressBar control in Flex

The following example shows how you can set the bar color on a Flex ProgressBar control by setting the barColor style.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/01/setting-the-bar-color-on-the-progressbar-control-in-flex/ -->
<mx:Application name="ProgressBar_barColor_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="barColor:">
                <mx:ColorPicker id="colorPicker"
                        selectedColor="red" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:ProgressBar id="progressBar"
            indeterminate="true"
            barColor="{colorPicker.selectedColor}"
            labelPlacement="center"
            height="100" />

</mx:Application>

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

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/01/setting-the-bar-color-on-the-progressbar-control-in-flex/ -->
<mx:Application name="ProgressBar_barColor_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Style>
        ProgressBar {
            barColor: red;
        }
    </mx:Style>

    <mx:ProgressBar id="progressBar"
            indeterminate="true"
            labelPlacement="center"
            height="100" />

</mx:Application>

If you’re setting the barColor style using CSS, you can also use the following syntax:

<mx:Style>
    ProgressBar {
        barColor: #FF0000;
    }
</mx:Style>

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

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/01/setting-the-bar-color-on-the-progressbar-control-in-flex/ -->
<mx:Application name="ProgressBar_barColor_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.events.ColorPickerEvent;

            private function colorPicker_change(evt:ColorPickerEvent):void {
                progressBar.setStyle("barColor", evt.color);
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="barColor:">
                <mx:ColorPicker id="colorPicker"
                        selectedColor="red"
                        change="colorPicker_change(event);" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:ProgressBar id="progressBar"
            indeterminate="true"
            labelPlacement="center"
            height="100" />

</mx:Application>

When setting the barColor style using ActionScript, you can also use the following syntaxes:

<mx:Script>
    <![CDATA[
        private function change():void {
            progressBar.setStyle("barColor", 0xFF0000);
        }
    ]]>
</mx:Script>
<mx:Script>
    <![CDATA[
        private function change():void {
            progressBar.setStyle("barColor", "#FF0000");
        }
    ]]>
</mx:Script>

5 thoughts on “Setting the bar color on the ProgressBar control in Flex

  1. When setting the bar colour of an indertimate progress bar, the resulting colour seems way off – for example, if you set the barColor to be #FF0000, it turns out as a pinkish colour. Thought that might be a good thing to mention in this article (maybe even how one can ‘solve’ this functionality!)!

Comments are closed.