Changing the background color of the DateChooser control in Flex

The following example shows how you can change the background color of a Flex DateChooser control by setting the backgroundColor and backgroundAlpha styles.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/05/02/changing-the-background-color-of-the-datechooser-control-in-flex/ -->
<mx:Application 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="backgroundAlpha:">
                <mx:HSlider id="slider"
                        minimum="0"
                        maximum="1"
                        value="1"
                        liveDragging="true"
                        snapInterval="0.01"
                        tickInterval="0.1" />
            </mx:FormItem>
            <mx:FormItem label="backgroundColor:">
                <mx:ColorPicker id="colorPicker"
                        selectedColor="white" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:DateChooser id="dateChooser"
            backgroundAlpha="{slider.value}"
            backgroundColor="{colorPicker.selectedColor}" />

</mx:Application>

View source is enabled in the following example.

Due to popular demand, here’s the “same” example, but in ActionScript instead of MXML:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/05/02/changing-the-background-color-of-the-datechooser-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();">

    <mx:Script>
        <![CDATA[
            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.controls.ColorPicker;
            import mx.controls.DateChooser;
            import mx.controls.HSlider;
            import mx.events.ColorPickerEvent;
            import mx.events.SliderEvent;

            private var appControlBar:ApplicationControlBar;
            private var form:Form;
            private var slider:HSlider;
            private var colorPicker:ColorPicker;
            private var dateChooser:DateChooser;

            private function init():void {
                slider = new HSlider();
                slider.minimum = 0;
                slider.maximum = 1;
                slider.value = 1;
                slider.liveDragging = true;
                slider.snapInterval = 0.01;
                slider.tickInterval = 0.1;
                slider.addEventListener(SliderEvent.CHANGE, slider_change);

                colorPicker = new ColorPicker();
                colorPicker.selectedColor = 0xFFFFFF;
                colorPicker.addEventListener(ColorPickerEvent.CHANGE, colorPicker_change);

                var alphaFormItem:FormItem = new FormItem();
                alphaFormItem.label = "backgroundAlpha:";
                alphaFormItem.addChild(slider);

                var colorFormItem:FormItem = new FormItem();
                colorFormItem.label = "backgroundColor:";
                colorFormItem.addChild(colorPicker);

                form = new Form();
                form.styleName = "plain";
                form.addChild(alphaFormItem);
                form.addChild(colorFormItem);

                appControlBar = new ApplicationControlBar();
                appControlBar.dock = true;
                appControlBar.addChild(form);
                addChild(appControlBar);

                dateChooser = new DateChooser();
                dateChooser.setStyle("backgroundAlpha", slider.value);
                dateChooser.setStyle("backgroundColor", colorPicker.selectedColor);
                addChild(dateChooser);
            }

            private function slider_change(evt:SliderEvent):void {
                dateChooser.setStyle("backgroundAlpha", evt.value);
            }

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

</mx:Application>

View source is enabled in the following example.

You can also set the backgroundColor and backgroundAlpha styles in an external .CSS file or within a <mx:Style /> block, as seen in the following snippet:

<mx:Style>
    DateChooser {
        backgroundColor: red;
        backgroundAlpha: 0.5;
    }
</mx:Style>

4 thoughts on “Changing the background color of the DateChooser control in Flex

  1. Hello peterd ,
    Just wanted to say thanks for all the hard work you have put into this blog.
    I was looking for this .

    Thanks again.

  2. Hey do you know how to change just the individual days background color?
    Not with selecteddate or a range of selected dates or disabled dates. Just specific dates background color.
    Thanks

Comments are closed.