Creating a toggleable MX LinkButton control in Flex 4

In a previous example, “Creating a toggleable LinkButton control in Flex”, we saw how you could create a toggleable Flex LinkButton control by extending the mx.skins.halo.LinkButtonSkin and adding custom “selectedUpSkin”, “selectedOverSkin”, “selectedDownSkin”, and “selectedDisabledSkin” skin states.

The following example shows how you can create a toggleable MX LinkButton control in Flex 4 by creating a custom MX LinkButton skin and specifying a background fill for the selected (“selectedUp”, “selectedOver”, “selectedDown”, and “selectedDisabled”) states.

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/04/creating-a-toggleable-mx-linkbutton-control-in-flex-4/ -->
<s:Application name="MX_LinkButton_toggle_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:VGroup horizontalCenter="0" verticalCenter="0">
        <mx:LinkButton id="lb1"
                label="Default ({lb1.selected})"
                toggle="true" />
        <mx:LinkButton id="lb2"
                label="Custom ({lb2.selected})"
                toggle="true"
                skin="skins.MyLinkButtonSkin" />
    </s:VGroup>
 
</s:Application>

[HaloSparkSkins]

And the custom MX LinkButton skin class, skins/MyLinkButtonSkin.mxml, is as follows:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/02/04/creating-a-toggleable-mx-linkbutton-control-in-flex-4/ -->
<s:Skin name="MyLinkButtonSkin"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        minWidth="21" minHeight="21"
        alpha.disabledStates="0.5">
 
    <!-- states -->
    <s:states>
        <s:State name="up" stateGroups="upStates"/>
        <s:State name="over" stateGroups="overStates"/>
        <s:State name="down" stateGroups="downStates" />
        <s:State name="disabled" stateGroups="disabledStates"/>
        <s:State name="selectedUp" stateGroups="selectedStates"/>
        <s:State name="selectedOver" stateGroups="selectedStates"/>
        <s:State name="selectedDown" stateGroups="selectedStates"/>
        <s:State name="selectedDisabled" stateGroups="selectedStates"/>
    </s:states>
 
    <!-- layer 1: fill -->
    <s:Rect left="0" right="0" top="0" bottom="0" 
            radiusX="{getStyle('cornerRadius')}"
            excludeFrom="upStates,disabledStates" >
        <s:fill>
            <s:SolidColor color.overStates="{getStyle('rollOverColor')}" 
                    color.downStates="{getStyle('selectionColor')}"
                    color.selectedStates="{getStyle('selectionColor')}"
                    alpha="1.0" />
        </s:fill>
    </s:Rect>
 
</s:Skin>

[GoogleAdsWide]

View source is enabled in the following example.

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.

7 thoughts on “Creating a toggleable MX LinkButton control in Flex 4

  1. Hi Peter,
    I’m trying to set the rollOverColor property on a Halo LinkButton in MXML, without success.
    e.g.,

    <mx:LinkButton label="Link" rollOverColor="#ff0000" />

    The rollover colour displays correctly in Flex SDK 3.5, but seems to be ignored in Flex 4 (Beta 2 release).
    Thanks,
    Chris

    1. @Chris,

      This code seems to work for me in Flex 4.0.0.14288. Rolling over the LinkButton control gives me a red background, pressing (and holding) the mouse button over the LinkButton control gives me an orange background:

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
              xmlns:s="library://ns.adobe.com/flex/spark"
              xmlns:mx="library://ns.adobe.com/flex/mx">
       
          <mx:LinkButton id="linkBtn"
                  rollOverColor="red"
                  selectionColor="haloOrange"
                  initialize="linkBtn.label = mx_internal::VERSION;" />
       
      </s:Application>

      Peter

      1. Cheers Peter,
        Works with build 4.0.0.14288 in Flex 3 Builder (I couldn’t compile in current Flash Builder beta because of the Halo namespace change).

        I hadn’t noticed the issue with hit area until now :)

Comments are closed.