Changing the background color of the DateChooser control in Flex

by Peter deHaan on May 2, 2008

in DateChooser

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 comments… read them below or add one }

1 Basem May 3, 2008 at 3:54 pm

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.

Reply

2 Gunjan kuamr September 20, 2008 at 5:14 am

It was really great.
But what about gradient color as background.

Is it possible ….

Reply

3 Nathan June 2, 2009 at 9:28 pm

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

Reply

4 Subash December 7, 2009 at 10:39 pm

Hey thanks for this amazing tutorial.

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: