Styling an emphasized Spark Button control in Flex 4

In a previous example, “Creating an emphasized Spark Button control in Flex 4”, we saw how you could set the emphasized button state on a Flex 4 Spark Button control by setting the Boolean emphasized property.

The following example shows how you can style the Spark Button control’s emphasized state by setting the Button.emphasized CSS selector.

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/03/03/styling-an-emphasized-fxbutton-control-in-flex-gumbo/ -->
<s:Application name="Spark_Button_emphasized_style_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">
    <s:controlBarContent>
        <s:CheckBox id="checkBox" label="emphasized" />
    </s:controlBarContent>
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/halo";
 
        s|Button.emphasized {
            baseColor: red;
        }
    </fx:Style>
 
    <s:Button label="Spark Button label"
            emphasized="{checkBox.selected}"
            horizontalCenter="0"
            verticalCenter="0" />
 
</s:Application>

If you only wanted to style the emphasized state of certain buttons, you could use Flex 4’s Advanced CSS styling to style by id, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/03/styling-an-emphasized-fxbutton-control-in-flex-gumbo/ -->
<s:Application name="Spark_Button_emphasized_style_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">
    <s:controlBarContent>
        <s:CheckBox id="checkBox" label="emphasized" selected="true" />
    </s:controlBarContent>
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/halo";
 
        s|Button#btn1.emphasized {
            baseColor: red;
        }
    </fx:Style>
 
    <s:VGroup horizontalCenter="0" verticalCenter="0">
        <s:Button id="btn1"
                label="Spark Button"
                emphasized="{checkBox.selected}" />
        <s:Button id="btn2"
                label="Spark Button"
                emphasized="{checkBox.selected}" />
    </s:VGroup>
 
</s:Application>

Or, you can style the emphasized state by setting the styleName property for certain buttons, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/03/styling-an-emphasized-fxbutton-control-in-flex-gumbo/ -->
<s:Application name="Spark_Button_emphasized_style_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">
    <s:controlBarContent>
        <s:CheckBox id="checkBox" label="emphasized" selected="true" />
    </s:controlBarContent>
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/halo";
 
        s|Button.myButton.emphasized {
            baseColor: red;
        }
    </fx:Style>
 
    <s:VGroup horizontalCenter="0" verticalCenter="0">
        <s:Button id="btn1"
                label="Spark Button"
                styleName="myButton"
                emphasized="{checkBox.selected}" />
        <s:Button id="btn2"
                label="Spark Button"
                styleName="myButton"
                emphasized="{checkBox.selected}" />
        <s:Button id="btn3"
                label="Spark Button"
                emphasized="{checkBox.selected}" />
    </s:VGroup>
 
</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.

One thought on “Styling an emphasized Spark Button control in Flex 4

  1. the check box actually sets the emphasized to true, making Spark Buttons look blueish ( the default perhaps?? ) and ignoring the base color being set to red,,,
    i am not sure if this used to work with a previous version of the flex 4 sdk, but its not with 4.1.0

    thank you for you code and time

Comments are closed.