Setting the background color on a Spark Button control in Flex 4

The following example shows how you can set the base color (or background color) on a Flex 4 Spark Button control by setting the baseColor style using MXML, CSS, or ActionScript.

Full code after the jump.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.

For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/05/setting-the-background-color-on-a-fxbutton-control-in-flex-gumbo/ -->
<s:Application name="Spark_Button_baseColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
 
    <mx:ApplicationControlBar width="100%" cornerRadius="0">
        <mx:Form styleName="plain">
            <mx:FormItem label="baseColor:">
                <mx:ColorPicker id="colorPicker"
                        selectedColor="#CCCCCC" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>
 
    <s:Button id="btn"
            label="Spark Button"
            baseColor="{colorPicker.selectedColor}"
            horizontalCenter="0" verticalCenter="0" />
 
</s:Application>

View source is enabled in the following example.

[GoogleAdsWide]

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/05/setting-the-background-color-on-a-fxbutton-control-in-flex-gumbo/ -->
<s:Application name="Spark_Button_baseColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/halo";
 
        s|Button {
            baseColor: red;
            /* baseColor: #FF0000; */
            /* baseColor: "0xFF0000"; */
        }
    </fx:Style>
 
    <s:Button id="btn"
            label="Spark Button"
            horizontalCenter="0" verticalCenter="0" />
 
</s:Application>

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/05/setting-the-background-color-on-a-fxbutton-control-in-flex-gumbo/ -->
<s:Application name="Spark_Button_baseColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
 
    <fx:Script>
        <![CDATA[
            import mx.events.ColorPickerEvent;
 
            protected function colorPicker_change(evt:ColorPickerEvent):void {
                btn.setStyle("baseColor", evt.color);
            }
        ]]>
    </fx:Script>
 
    <mx:ApplicationControlBar width="100%" cornerRadius="0">
        <mx:Form styleName="plain">
            <mx:FormItem label="baseColor:">
                <mx:ColorPicker id="colorPicker"
                        selectedColor="#CCCCCC"
                        change="colorPicker_change(event);"/>
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>
 
    <s:Button id="btn"
            label="Spark Button"
            horizontalCenter="0" verticalCenter="0" />
 
</s:Application>

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/05/setting-the-background-color-on-a-fxbutton-control-in-flex-gumbo/ -->
<s:Application name="Spark_Button_baseColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo"
        initialize="init();">
 
    <fx:Script>
        <![CDATA[
            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.controls.ColorPicker;
            import mx.events.ColorPickerEvent;
            import spark.components.Button;
 
            private var colorPicker:ColorPicker;
            private var btn:Button;
 
            private function init():void {
                colorPicker = new ColorPicker();
                colorPicker.selectedColor = 0xCCCCCC;
                colorPicker.addEventListener(ColorPickerEvent.CHANGE, colorPicker_change);
 
                var formItem:FormItem = new FormItem();
                formItem.label = "baseColor:";
                formItem.addElement(colorPicker);
 
                var form:Form = new Form();
                form.styleName = "plain";
                form.addElement(formItem);
 
                var appControlBar:ApplicationControlBar = new ApplicationControlBar();
                appControlBar.percentWidth = 100;
                appControlBar.setStyle("cornerRadius", 0);
                appControlBar.addElement(form);
                addElementAt(appControlBar, 0);
 
                btn = new Button();
                btn.label = "Spark Button";
                btn.horizontalCenter = 0;
                btn.verticalCenter = 0;
                addElement(btn);
            }
 
            protected function colorPicker_change(evt:ColorPickerEvent):void {
                btn.setStyle("baseColor", evt.color);
            }
        ]]>
    </fx:Script>
 
</s:Application>

Finally, you could set the Spark Button control’s baseColor style globally using the static StyleManager.getStyleDeclaration() and setStyle() methods, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/05/setting-the-background-color-on-a-fxbutton-control-in-flex-gumbo/ -->
<s:Application name="Spark_Button_baseColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo"
        initialize="init();">
 
    <fx:Script>
        <![CDATA[
            import spark.components.Button;
 
            private var btn:Button;
 
            private function init():void {
                var btnStyles:CSSStyleDeclaration = StyleManager.getStyleDeclaration("spark.components.Button");
                btnStyles.setStyle("baseColor", "haloBlue");
 
                btn = new Button();
                btn.label = "Spark Button";
                btn.horizontalCenter = 0;
                btn.verticalCenter = 0;
                addElement(btn);
            }
        ]]>
    </fx:Script>
 
</s:Application>

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

3 thoughts on “Setting the background color on a Spark Button control in Flex 4

Comments are closed.