Setting a custom skin on a Spark Button control in Flex 4

The following example shows how you can set a custom skin on an Spark Button control in Flex 4 by setting the skinClass 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/2009/03/04/setting-a-custom-skin-on-an-fxbutton-control-in-flex-gumbo/ -->
<s:Application name="Spark_Button_skinClass_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:layout>
        <s:BasicLayout />
    </s:layout>
 
    <s:Button label="Spark Button"
            skinClass="skins.CustomButtonSkin"
            left="300" top="30" />
 
</s:Application>

And the custom Spark Button skin, skins/CustomButtonSkin.mxml, is as follows:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/04/setting-a-custom-skin-on-an-fxbutton-control-in-flex-gumbo/ -->
<s:SparkSkin name="CustomButtonSkin"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:fb="http://ns.adobe.com/flashbuilder/2009"
        minWidth="21" minHeight="21"
        alpha.disabled="0.5">
    <!-- states -->
    <s:states>
        <s:State name="up" />
        <s:State name="over" />
        <s:State name="down" />
        <s:State name="disabled" />
    </s:states>
 
    <!-- host component -->
    <fx:Metadata>
        [HostComponent("spark.components.Button")]
    </fx:Metadata>
 
    <s:Path data.up="M 0 0 L 10 0 L 10 10 L 0 10 Z"
          data.over="M 0 0 L 20 0 L 20 20 L 0 20 Z"
          data.down="M 0 0 L 30 0 L 30 30 L 0 30 Z">
        <s:fill>
            <s:SolidColor color="#000000" 
                          color.down="red"/>
        </s:fill>
    </s:Path>
 
</s:SparkSkin>

[GoogleAdsWide]

View source is enabled in the following example.

You can also set the skinClass style in an external .CSS file or <Style/> block, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/04/setting-a-custom-skin-on-an-fxbutton-control-in-flex-gumbo/ -->
<s:Application name="Spark_Button_skinClass_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:layout>
        <s:BasicLayout />
    </s:layout>
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/mx";
 
        s|Button {
            skinClass: ClassReference("skins.CustomButtonSkin");
        }
    </fx:Style>
 
    <s:Button label="Spark Button"
            left="300" top="30" />
 
</s:Application>

Or, you can set the skinClass style using ActionScript, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/04/setting-a-custom-skin-on-an-fxbutton-control-in-flex-gumbo/ -->
<s:Application name="Spark_Button_skinClass_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:layout>
        <s:BasicLayout />
    </s:layout>
 
    <fx:Script>
        <![CDATA[
            import skins.CustomButtonSkin;
 
            protected function button1_clickHandler(evt:MouseEvent):void {
                button1.setStyle("skinClass", CustomButtonSkin);
            }
        ]]>
    </fx:Script>
 
 
    <s:Button id="button1"
            label="Spark Button"
            left="300" top="30"
            click="button1_clickHandler(event);" />
 
</s:Application>

Finally, you can also define the skin directly in the MXML file by wrapping the Skin object in a <Component/> tag within the <Declarations/> section, as seen in the following example (big thanks to my co-worker Ryan Frishberg for the heads up on this solution):

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/04/setting-a-custom-skin-on-an-fxbutton-control-in-flex-gumbo/ -->
<s:Application name="Spark_Button_skinClass_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:layout>
        <s:BasicLayout />
    </s:layout>
 
    <fx:Declarations>
        <fx:Component className="InlineCustomButtonSkin">
            <s:SparkSkin name="CustomButtonSkin"
                    xmlns:fx="http://ns.adobe.com/mxml/2009"
                    xmlns:s="library://ns.adobe.com/flex/spark" 
                    xmlns:fb="http://ns.adobe.com/flashbuilder/2009"
                    minWidth="21" minHeight="21"
                    alpha.disabled="0.5">
                <!-- states -->
                <s:states>
                    <s:State name="up" />
                    <s:State name="over" />
                    <s:State name="down" />
                    <s:State name="disabled" />
                </s:states>
 
                <!-- host component -->
                <fx:Metadata>
                    [HostComponent("spark.components.Button")]
                </fx:Metadata>
 
                <s:Path data.up="M 0 0 L 10 0 L 10 10 L 0 10 Z"
                        data.over="M 0 0 L 20 0 L 20 20 L 0 20 Z"
                        data.down="M 0 0 L 30 0 L 30 30 L 0 30 Z">
                    <s:fill>
                        <s:SolidColor color="#000000" 
                                      color.down="red"/>
                    </s:fill>
                </s:Path>
            </s:SparkSkin>
        </fx:Component>
    </fx:Declarations>
 
    <s:Button label="Spark Button"
              skinClass="InlineCustomButtonSkin"
              left="300" top="30" />
 
</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 a custom skin on a Spark Button control in Flex 4

  1. Why don’t you provide the working sample any more? I know, not every player will be able to see it, but maybe you coud do it anyway. like that it is much easier and faster to understand what you are demonstrating and you do not have to put the code in a project first…

  2. Fan,

    I haven’t been doing the SWF examples lately since the Flex Gumbo Fx namespace is going to be removed, so all of these examples will need to be updated. So, I didn’t want to go to the effort of creating/uploading SWFs for these examples when I’ll probably have to replace them later anyways. Especially considering it is fairly easy to copy/paste in Flex Builder and see what the code does.

    When/if I update all these examples after Flex Gumbo is released I’ll add SWFs.

    Peter

  3. Peter –

    I was trying this from FB4, but i’m seeing run time errors as i mouse over the box. I’ve copied the main app and skin class directly from your first two examples above. Does this seem like its likely to be a version difference of the SDK?

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at mx.graphics::Path/set data() [E:\dev\i10\frameworks\projects\flex4\src\mx\graphics\Path.as:225]
    at mx.states::SetProperty/setPropertyValue() [E:\dev\i10\frameworks\projects\framework\src\mx\states\SetProperty.as:341]
    at mx.states::SetProperty/remove() [E:\dev\i10\frameworks\projects\framework\src\mx\states\SetProperty.as:311]
    at mx.core::UIComponent/removeState() [E:\dev\i10\frameworks\projects\framework\src\mx\core\UIComponent.as:7992]
    at mx.core::UIComponent/commitCurrentState() [E:\dev\i10\frameworks\projects\framework\src\mx\core\UIComponent.as:7781]
    at mx.core::UIComponent/setCurrentState() [E:\dev\i10\frameworks\projects\framework\src\mx\core\UIComponent.as:7692]
    at mx.core::UIComponent/set currentState() [E:\dev\i10\frameworks\projects\framework\src\mx\core\UIComponent.as:4686]
    at mx.components.baseClasses::FxComponent/commitProperties() [E:\dev\i10\frameworks\projects\flex4\src\mx\components\baseClasses\FxComponent.as:213]
    at mx.components::FxButton/commitProperties() [E:\dev\i10\frameworks\projects\flex4\src\mx\components\FxButton.as:464]
    at mx.core::UIComponent/validateProperties() [E:\dev\i10\frameworks\projects\framework\src\mx\core\UIComponent.as:6167]
    at mx.managers::LayoutManager/validateProperties() [E:\dev\i10\frameworks\projects\framework\src\mx\managers\LayoutManager.as:539]
    at mx.managers::LayoutManager/doPhasedInstantiation() [E:\dev\i10\frameworks\projects\framework\src\mx\managers\LayoutManager.as:689]
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at mx.core::UIComponent/callLaterDispatcher2() [E:\dev\i10\frameworks\projects\framework\src\mx\core\UIComponent.as:9142]
    at mx.core::UIComponent/callLaterDispatcher() [E:\dev\i10\frameworks\projects\framework\src\mx\core\UIComponent.as:9082]

Comments are closed.