Setting an icon on a Spark Button in Flex Hero

The following example shows how you can set the icon on a Spark Button in Flex Hero by setting the icon style.

The following example(s) require Flash Player 10 and the beta Adobe Flex Hero SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex Hero SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+Hero.

The following example requires Flex SDK 4.5.0.18623 or later.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/12/17/setting-an-icon-on-a-spark-button-in-flex-hero/ -->
<s:Application name="Spark_Button_icon_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:VerticalLayout horizontalAlign="center" verticalAlign="middle" />
    </s:layout>
 
    <fx:Script>
        <![CDATA[
            [Embed("assets/asterisk_orange.png")]
            protected const ICON:Class;
        ]]>
    </fx:Script>
 
    <s:Button label="Spark Button with dynamic icon" icon="assets/asterisk_orange.png" />
    <s:Button label="Spark Button with inline embedded icon" icon="@Embed('assets/asterisk_orange.png')" />
    <s:Button label="Spark Button with metadata [Embed] icon" icon="{ICON}" />
 
</s:Application>

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/12/17/setting-an-icon-on-a-spark-button-in-flex-hero/ -->
<s:Application name="Spark_Button_icon_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:VerticalLayout horizontalAlign="center" verticalAlign="middle" />
    </s:layout>
 
    <fx:Script>
        <![CDATA[
            [Embed("assets/asterisk_orange.png")]
            protected const ICON:Class;
        ]]>
    </fx:Script>
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/mx";
 
        s|Button#btn1 {
            icon: "assets/asterisk_orange.png";
        }
 
        s|Button#btn2 {
            icon: Embed("assets/asterisk_orange.png");
        }
 
        s|Button#btn3 {
            icon: PropertyReference("ICON");
        }
    </fx:Style>
 
    <s:Button id="btn1" label="Spark Button with dynamic icon" />
    <s:Button id="btn2" label="Spark Button with inline embedded icon" />
    <s:Button id="btn3" label="Spark Button with metadata [Embed] icon" />
 
</s:Application>

Or, you can set the icon property using ActionScript, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/12/17/setting-an-icon-on-a-spark-button-in-flex-hero/ -->
<s:Application name="Spark_Button_icon_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"
        initialize="init();">
    <s:layout>
        <s:VerticalLayout horizontalAlign="center" verticalAlign="middle" />
    </s:layout>
 
    <fx:Script>
        <![CDATA[
            [Embed("assets/asterisk_orange.png")]
            protected const ICON:Class;
 
            protected function init():void {
                btn1.setStyle("icon", "assets/asterisk_orange.png");
                btn2.setStyle("icon", ICON);
            }
        ]]>
    </fx:Script>
 
    <s:Button id="btn1" label="Spark Button with dynamic icon" />
    <s:Button id="btn2" label="Spark Button with an embedded icon" />
 
</s:Application>

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

7 thoughts on “Setting an icon on a Spark Button in Flex Hero

      1. And it will require Flash 10.2 to be installed :) all that to get what we’ve had for years in Flex 3.

        Pardon the pessimism, I really am glad these basic usability features are back, but I still don’t agree with decision to make 4.5 final a Flash 10.2 only (unless that’s changed).

  1. Any idea how to use an fxg as an icon? Given the vast array of screen sizes around using vector graphics is the only realistic solution.

  2. i have a question. i designed a custom theme and embedded it in my project. when i define the icon with the stylesheets all styles are overidden but i would like to have the backgorund etc. to stay the same do you have any idea how this can be managed?

    thanks

Comments are closed.