The following example shows how you can change the background fill colors and fill alphas of a Button control in Flex by setting the fillColors, and fillAlphas styles.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/02/changing-the-fill-colors-of-a-button-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Button id="button2"
            label="2 fill colors defined"
            fillColors="[red, haloOrange]" />

    <mx:Button id="button4"
            label="4 fill colors defined"
            fillColors="[red, haloOrange, haloGreen, haloBlue]" />

    <mx:Button id="buttonSolid"
            label="solid fill"
            fillColors="[red, red]" />

    <mx:Button id="buttonSolidOpaque"
            label="solid opaque fill"
            fillAlphas="[1.0, 1.0]"
            fillColors="[red, red]" />

</mx:Application>

View source is enabled in the following example.

You can also set the fillColors and fillAlphas styles using an external .CSS file or <mx:Style /> block, as shown in the following snippet:

<mx:Style>
    Button {
        fillAlphas: 1.0, 0.4;
        fillColors: haloBlue, haloGreen;
    }
</mx:Style>

<mx:Button id="button" label="Button" />

Or, you can set the fillColors and fillAlphas styles using ActionScript, as shown in the following snippet:

<mx:Script>
    <![CDATA[
        private function init():void {
            button.setStyle("fillAlphas", [1.0, 0.4]);
            button.setStyle("fillColors", ["haloBlue", "haloGreen"]);
        }
    ]]>
</mx:Script>

<mx:Button id="button" label="Button" initialize="init();" />

For an example of changing the fillColors and fillAlphas styles for a ButtonBar control, see “Changing the fill colors of a ButtonBar control in Flex”.

The default values for the Button fillAlphas style in Flex 3 is: [0.6, 0.4, 0.75, 0.65], and the default value for the fillColors style is: [#FFFFFF, #CCCCCC, #FFFFFF, #EEEEEE].

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

One Response to Changing the fill colors of a Button control in Flex

  1. carlos says:

    how can we do this in sparks?

Leave a Reply

Your email address will not be published.

You may 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