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

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 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/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>

[GoogleAdsWide]

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.

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

  1. Hi

    i am able to create multiline radio button.
    but my problem is i need to place that radio button visuval component i.e circle at 1st line

    i had 3 lines of text i need to place that radio button circle at 1st line

    please help me to solve this problem

    Regards
    Mahesh

    1. @Mahesh,

      For an example of multiline Spark RadioButton control with a vertically aligned radio button circle, see “Creating a multiline Spark RadioButton control in Flex 4”.

      It is basically the default Spark RadioButton skin, but I had to make two minor adjustments:
      (1) To enable multiline labels, I removed the maxDisplayedLines property.
      (2) To vertically align the radio button dot, I had to change this line (~line 89 in the default RadioButtonSkin.mxml):

      <s:Group verticalCenter="0" width="13" height="13">

      With this line:

      <s:Group top="5" width="13" height="13">

      Hope that helps,
      Peter

Comments are closed.