Setting the symbol color on a Spark RadioButton control in Flex 4

by Peter deHaan on February 5, 2010

in RadioButton (Spark), RadioButtonGroup (Spark), beta2

The following example shows how you can set the symbol/bullet color on a Spark RadioButton control in Flex 4 by setting the symbolColor style.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 beta, check out the Adobe Flash Builder 4 page on the Adobe Labs site. To download the latest build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4. For instructions on using the beta Flex 4 SDK in Flex Builder 3, see "Using the beta Flex 4 SDK in Flex Builder 3".

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/02/05/setting-the-symbol-color-on-a-spark-radiobutton-control-in-flex-4/ -->
<s:Application name="Spark_RadioButton_symbolColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:controlBarContent>
        <mx:Form>
            <mx:FormItem label="symbolColor:">
                <mx:ColorPicker id="cp" selectedColor="black" />
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <fx:Declarations>
        <s:RadioButtonGroup id="radioGr" />
    </fx:Declarations>
 
    <s:VGroup horizontalCenter="0" verticalCenter="0">
        <s:RadioButton id="rb1"
                label="Red"
                group="{radioGr}"
                selected="true"
                symbolColor="{cp.selectedColor}"/>
        <s:RadioButton id="rb2"
                label="Orange"
                group="{radioGr}"
                symbolColor="{cp.selectedColor}" />
        <s:RadioButton id="rb3"
                label="Yellow"
                group="{radioGr}"
                symbolColor="{cp.selectedColor}" />
        <s:RadioButton id="rb4"
                label="Green"
                group="{radioGr}"
                symbolColor="{cp.selectedColor}" />
        <s:RadioButton id="rb5"
                label="Blue"
                group="{radioGr}"
                symbolColor="{cp.selectedColor}" />
    </s:VGroup>
 
</s:Application>

View source is enabled in the following example.

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/02/05/setting-the-symbol-color-on-a-spark-radiobutton-control-in-flex-4/ -->
<s:Application name="Spark_RadioButton_symbolColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/mx";
 
        s|RadioButton {
            symbolColor: red;
        }
    </fx:Style>
 
    <fx:Declarations>
        <s:RadioButtonGroup id="radioGr" />
    </fx:Declarations>
 
    <s:VGroup horizontalCenter="0" verticalCenter="0">
        <s:RadioButton id="rb1"
                label="Red"
                group="{radioGr}"
                selected="true" />
        <s:RadioButton id="rb2"
                label="Orange"
                group="{radioGr}" />
        <s:RadioButton id="rb3"
                label="Yellow"
                group="{radioGr}" />
        <s:RadioButton id="rb4"
                label="Green"
                group="{radioGr}" />
        <s:RadioButton id="rb5"
                label="Blue"
                group="{radioGr}" />
    </s:VGroup>
 
</s:Application>

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/02/05/setting-the-symbol-color-on-a-spark-radiobutton-control-in-flex-4/ -->
<s:Application name="Spark_RadioButton_symbolColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:controlBarContent>
        <mx:Form>
            <mx:FormItem label="symbolColor:">
                <mx:ColorPicker id="cp"
                        selectedColor="black"
                        change="cp_changeHandler(event);" />
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            import mx.events.ColorPickerEvent;
 
            protected function cp_changeHandler(evt:ColorPickerEvent):void {
                rb1.setStyle("symbolColor", evt.color);
                rb2.setStyle("symbolColor", evt.color);
                rb3.setStyle("symbolColor", evt.color);
                rb4.setStyle("symbolColor", evt.color);
                rb5.setStyle("symbolColor", evt.color);
            }
        ]]>
    </fx:Script>
 
    <fx:Declarations>
        <s:RadioButtonGroup id="radioGr" />
    </fx:Declarations>
 
    <s:VGroup horizontalCenter="0" verticalCenter="0">
        <s:RadioButton id="rb1"
                label="Red"
                group="{radioGr}"
                selected="true" />
        <s:RadioButton id="rb2"
                label="Orange"
                group="{radioGr}" />
        <s:RadioButton id="rb3"
                label="Yellow"
                group="{radioGr}" />
        <s:RadioButton id="rb4"
                label="Green"
                group="{radioGr}" />
        <s:RadioButton id="rb5"
                label="Blue"
                group="{radioGr}" />
    </s:VGroup>
 
</s:Application>

Finally you can set the symbolColor globally for the Spark RadioButton control by using the StyleManager, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/02/05/setting-the-symbol-color-on-a-spark-radiobutton-control-in-flex-4/ -->
<s:Application name="Spark_RadioButton_symbolColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:controlBarContent>
        <mx:Form>
            <mx:FormItem label="symbolColor:">
                <mx:ColorPicker id="cp"
                        selectedColor="black"
                        change="cp_changeHandler(event);" />
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            import mx.events.ColorPickerEvent;
 
            protected function cp_changeHandler(evt:ColorPickerEvent):void {
                var radioBtnCSS:CSSStyleDeclaration = styleManager.getStyleDeclaration("spark.components.RadioButton");
                radioBtnCSS.setStyle("symbolColor", evt.color);
            }
        ]]>
    </fx:Script>
 
    <fx:Declarations>
        <s:RadioButtonGroup id="radioGr" />
    </fx:Declarations>
 
    <s:VGroup horizontalCenter="0" verticalCenter="0">
        <s:RadioButton id="rb1"
                label="Red"
                group="{radioGr}"
                selected="true" />
        <s:RadioButton id="rb2"
                label="Orange"
                group="{radioGr}" />
        <s:RadioButton id="rb3"
                label="Yellow"
                group="{radioGr}" />
        <s:RadioButton id="rb4"
                label="Green"
                group="{radioGr}" />
        <s:RadioButton id="rb5"
                label="Blue"
                group="{radioGr}" />
    </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.

{ 2 comments… read them below or add one }

1 yuxuan2658 February 9, 2010 at 11:59 pm

xmlns:mx="library://ns.adobe.com/flex/mx" shoud be xmlns:mx="library://ns.adobe.com/flex/halo" !!!

Reply

2 Peter deHaan February 10, 2010 at 7:33 am

@yuxuan2658,

The namespace was renamed after beta 2 (in 4.0.0.12635, to be exact). For more information, see “The Flex 4 ‘halo’ namespace is now the ‘mx’ namespace”.

Peter

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: