Creating a toggleable MX LinkButton control in Flex 4

by Peter deHaan on February 4, 2010

in LinkButton, beta2

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 }

1 JabbyPanda February 23, 2010 at 7:30 am

Dropbox had mangled the uploaded SWF file :-/

Reply

2 Peter deHaan February 23, 2010 at 7:35 am

@JabbyPanda,

The SWF looks right to me (Win7/Chrome). What looks mangled on your end?

Peter

Reply

3 JabbyPanda February 23, 2010 at 8:17 am

Hi Peter, looks like it Dropbox fault, the service went down for couple of minutes, everything is back to normal now.

4 Chris March 1, 2010 at 2:09 pm

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

Reply

5 Peter deHaan March 1, 2010 at 3:33 pm

@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

Reply

6 Peter deHaan March 1, 2010 at 4:09 pm

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

Reply

7 Chris March 2, 2010 at 7:59 am

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