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 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".
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\*.
<?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>
And the custom 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.

{ 7 comments… read them below or add one }
Dropbox had mangled the uploaded SWF file :-/
@JabbyPanda,
The SWF looks right to me (Win7/Chrome). What looks mangled on your end?
Peter
Hi Peter, looks like it Dropbox fault, the service went down for couple of minutes, everything is back to normal now.
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
@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:
Peter
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
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 :)