Setting a custom thumb skin on a FxHSlider control in Flex Gumbo

by Peter deHaan on March 10, 2009

in FxHSlider, beta

The following example shows how you can create a custom thumb icon for the Flex Gumbo FxHSlider control by creating a new custom skin and setting the skinClass style.

Full code after the jump.

To use the following code, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3. For more information on downloading and installing the Gumbo SDK into Flex Builder 3, see “Using the beta Gumbo SDK in Flex Builder 3″.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/10/setting-a-custom-thumb-skin-on-a-fxhslider-control-in-flex-gumbo/ -->
<FxApplication name="FxHSlider_skinClass_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        backgroundColor="white">
    <layout>
        <BasicLayout />
    </layout>

    <FxHSlider id="slider"
            showDataTip="false"
            horizontalCenter="0"
            verticalCenter="0"
            skinClass="CustomFxHSliderSkin" />

</FxApplication>

Next, we create a custom skin for the slider thumb:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/10/setting-a-custom-thumb-skin-on-a-fxhslider-control-in-flex-gumbo/ -->
<SparkSkin name="BitmapGraphicThumbSkin"
        xmlns="http://ns.adobe.com/mxml/2009">

    <states>
        <State name="up" />
        <State name="over" />
        <State name="down" />
        <State name="disabled" />
    </states>

    <Metadata>
        [HostComponent("mx.components.FxButton")]
    </Metadata>

    <BitmapGraphic id="bg"
            source="@Embed('asterisk_yellow.png')"
            left="-4"
            top="-4" />

</SparkSkin>

Finally, we create a custom skin for the FxHSlider control, CustomFxHSliderSkin.mxml, which includes the custom slider thumb skin, BitmapGraphicThumbSkin.mxml:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/10/setting-a-custom-thumb-skin-on-a-fxhslider-control-in-flex-gumbo/ -->
<SparkSkin name="CustomFxHSliderSkin"
        xmlns="http://ns.adobe.com/mxml/2009"
        minHeight="11"
        minWidth="100"
        alpha.disabled="0.5">

    <Metadata>
        [HostComponent("mx.components.FxHSlider")]
    </Metadata>

    <Script>
        static private const exclusions:Array = ["track", "thumb"];
        override public function get colorizeExclusions():Array {return exclusions;}
    </Script>

    <states>
        <State name="normal" />
        <State name="disabled" />
    </states>

    <Declarations>
        <Component id="dataTip">
            <MXMLComponent minHeight="24" minWidth="40" y="-34">
                <Rect top="0" left="0" right="0" bottom="0">
                    <fill>
                        <SolidColor color="0x000000" alpha=".9"/>
                    </fill>
                    <filters>
                        <DropShadowFilter angle="90" color="0x999999" distance="3"/>
                    </filters>
                </Rect>

                <TextBox id="labelElement" text="{data}"
                         horizontalCenter="0" verticalCenter="1"
                         left="5" right="5" top="5" bottom="5"
                         textAlign="center" verticalAlign="middle"
                         fontWeight="normal" color="white" fontSize="11">
                </TextBox>
            </MXMLComponent>
        </Component>
    </Declarations>

    <FxButton id="track" left="0" right="0" top="0" bottom="0"
              skinClass="mx.skins.spark.FxHSliderTrackSkin" />
    <FxButton id="thumb" top="0" bottom="0" width="11" height="11"
              skinClass="BitmapGraphicThumbSkin" />

</SparkSkin>

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

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/10/setting-a-custom-thumb-skin-on-a-fxhslider-control-in-flex-gumbo/ -->
<FxApplication name="FxHSlider_skinClass_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        backgroundColor="white">
    <layout>
        <BasicLayout />
    </layout>

    <Style>
        FxHSlider {
            skinClass: ClassReference("CustomFxHSliderSkin");
        }
    </Style>

    <FxHSlider id="slider"
            showDataTip="false"
            horizontalCenter="0"
            verticalCenter="0" />

</FxApplication>

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

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/10/setting-a-custom-thumb-skin-on-a-fxhslider-control-in-flex-gumbo/ -->
<FxApplication name="FxHSlider_skinClass_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        backgroundColor="white">
    <layout>
        <BasicLayout />
    </layout>

    <FxButton label="Set skin class"
            click="slider.setStyle('skinClass', CustomFxHSliderSkin);"
            left="10"
            top="10" />

    <FxHSlider id="slider"
            showDataTip="false"
            horizontalCenter="0"
            verticalCenter="0" />

</FxApplication>

This entry is based on a beta version of the Flex Gumbo 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 Gumbo SDK.

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: