Setting the content background color on a Spark TextInput control in Flex Gumbo

The following example shows how you can set the content background color of a Flex Gumbo Spark TextInput control by setting the contentBackgroundColor 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/2008/12/19/setting-the-content-color-on-an-fxtextinput-control-in-flex-gumbo/ -->
<s:Application name="Spark_TextInput_contentBackgroundColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">

    <mx:ApplicationControlBar width="100%" cornerRadius="0">
        <mx:Form styleName="plain">
            <mx:FormItem label="contentBackgroundColor:">
                <mx:ColorPicker id="colorPicker"
                        selectedColor="#FFFFFF" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <s:TextInput id="textInput"
            text="The quick brown fox jumps over the lazy dog"
            contentBackgroundColor="{colorPicker.selectedColor}"
            horizontalCenter="0"
            verticalCenter="0" />

</s:Application>

View source is enabled in the following example.

You can also set the contentBackgroundColor 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/2008/12/19/setting-the-content-color-on-an-fxtextinput-control-in-flex-gumbo/ -->
<s:Application name="Spark_TextInput_contentBackgroundColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">

    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";

        s|TextInput {
            contentBackgroundColor: #FFFF99;
        }
    </fx:Style>

    <s:TextInput id="textInput"
            text="The quick brown fox jumps over the lazy dog"
            horizontalCenter="0"
            verticalCenter="0" />

</s:Application>

You can also set the contentBackgroundColor style using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/12/19/setting-the-content-color-on-an-fxtextinput-control-in-flex-gumbo/ -->
<s:Application name="Spark_TextInput_contentBackgroundColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">

    <fx:Script>
        <![CDATA[
            import mx.events.ColorPickerEvent;

            private function colorPicker_change(evt:ColorPickerEvent):void {
                textInput.setStyle("contentBackgroundColor", evt.color);
            }
        ]]>
    </fx:Script>

    <mx:ApplicationControlBar width="100%" cornerRadius="0">
        <mx:Form styleName="plain">
            <mx:FormItem label="contentBackgroundColor:">
                <mx:ColorPicker id="colorPicker"
                        selectedColor="#FFFFFF"
                        change="colorPicker_change(event);" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <s:TextInput id="textInput"
            text="The quick brown fox jumps over the lazy dog"
            horizontalCenter="0"
            verticalCenter="0" />

</s:Application>

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.

4 thoughts on “Setting the content background color on a Spark TextInput control in Flex Gumbo

  1. hmm i get : Multiple markers at this line: -CSS type selectors are not supported in components: ‘spark.components.TextInput’ -CSS type selectors are
    not supported in components: ‘spark.components.TextInput’

    when applying the css style

    1. @innur,

      Which version/build of the Flex SDK are you using?
      I just tested all 3 examples w/ 4.0.0.13963 and they all worked fine (although I did have to change the “halo” namespace URL to “mx”).

      Peter

Comments are closed.