tags:

views:

58

answers:

2

What is the easiest way to change the background color of a toggle button when it is selected?

I've tried creating a custom skin for the button and applying it to the downSkin property, but I can't figure out how to change the background color from within the skin. Also I'd like to avoid using an image as a background if possible.

A: 

No easy way. Your best bet is to open up Flash Professional and create a skin; then assign that skin to the button as a style using something like this:

MyButton.setStyle('skin', mySkin);

[This advice may not apply if you are referring to a Spark ToggleButton as opposed to a Halo ToggleButton[

www.Flextras.com
+1  A: 

I hope I understand what you need.

In your skin file, the state upAndSelected is what you need to specify the color when the togglebutton is selected. You can copy the generated skin code from Flex to modify it or check the code below. Add it below the </s:Rect> tag under <!--layer 2: fill @private-->`

<s:Rect id="fill2" left="1" right="1" top="1" bottom="1" radiusX="2">
    <s:fill>
        <s:LinearGradient rotation="90">
        <s:GradientEntry color="0xFFFFFF"
                         color.upAndSelected="#333333" // you can modify color here
                         alpha="0.85" 
                         />

        <s:GradientEntry color="0xD8D8D8" 
                         color.upAndSelected="red" // you can modify color here 
                         alpha.downAndSelected="1" />
        </s:LinearGradient>
    </s:fill>
</s:Rect>

Paste the code above under

 <!-- layer 2: fill -->
    <!--- @private -->
    <s:Rect id="fill" left="1" right="1" top="1" bottom="1" radiusX="2">
        <s:fill>
            <s:LinearGradient rotation="90">
                <s:GradientEntry color="0xFFFFFF" 
                               color.selectedUpStates="0xBBBDBD"
                               color.overStates="0xBBBDBD" 
                               color.downStates="0xAAAAAA" 
                               alpha="0.85" 
                               alpha.overAndSelected="1" />
                <s:GradientEntry color="0xD8D8D8" 
                               color.selectedUpStates="0x9FA0A1"
                               color.over="0x9FA0A1" 
                               color.overAndSelected="0x8E8F90"
                               color.downStates="0x929496" 
                               alpha="0.85"
                               alpha.overAndSelected="1" />
            </s:LinearGradient>
        </s:fill>
    </s:Rect>

Hope it helps.

Jerry
I'm not sure what you mean by "generated skin code". Does the Rect definition go under the <mx:State name="selectedUp"> element or in the main body of the skin UIComponent?
Mike Deck
Sorry about the confusion. I just realize my response was cut off... Inside your <s:togglebutton> component, type skinclass and hit Ctrl+Enter (PC) will give you a popup window with bunch of options. click create skin will generate a skin file for you. You can modify it for what you need. Also, see my updated code above.
Jerry