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 http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://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>

The default Spark skins for the MX/Halo controls/containers can be found in the Flex SDK at:
%Flex SDK%\frameworks\projects\sparkskins\src\mx\skins\spark\*.

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>

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.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

7 Responses to Creating a toggleable MX LinkButton control in Flex 4

  1. JabbyPanda says:

    Dropbox had mangled the uploaded SWF file :-/

  2. Chris says:

    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

    • Peter deHaan says:

      @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

    • Peter deHaan says:

      Although I did notice this issue; http://bugs.adobe.com/jira/browse/SDK-25707

      Not sure if you’re running in to that. Looks like the MX LinkButton control’s skin’s up state doesnt handle non-default sized hit areas very well.

      Peter

      • Chris says:

        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 :)

Leave a Reply

Your email address will not be published.

You may 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