Setting a Flex application’s style name

by Peter deHaan on September 18, 2007

in Application

The following example shows how you can use the styleName style in a Flex Application to remove the background image, set the background color to white and left aligns the content.

To quote the Flex 2.0.1 documentation, “Overriding the default Application container styles“:

The Flex default style sheet defines a plain style name that sets all padding to 0 pixels, removes the default background image, sets the background color to white, and left-aligns the children.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/09/18/setting-a-flex-applications-style-name/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle">

    <mx:Script>
        <![CDATA[
            private function plainStyle():void {
                Application.application.setStyle("styleName", "plain");
            }

            private function defaultStyle():void {
                Application.application.setStyle("styleName", undefined);
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Button label="styleName = 'plain'"
                click="plainStyle();" />
        <mx:Button label="styleName = undefined"
                click="defaultStyle();" />
    </mx:ApplicationControlBar>

    <mx:Button label="Button 1" />
    <mx:Button label="Button 2" />
    <mx:Button label="Button 3" />

</mx:Application>

View source is enabled in the following example.

As you can see in the previous example, when setting the Application’s styleName style to “plain”, the content becomes left aligned. If you want to center the content horizontally within the application, set the <mx:Application /> tag’s horizontalAlign style to “center”, as seen in the following snippet:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        horizontalAlign="center">
    ...
</mx:Application>

Note that you can also set the styleName style directly in the <mx:Application /> tag by using the following snippet:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        styleName="plain">
    ...
</mx:Application>

OK, looking a bit closer, it seems you can also set the styleName as a property, which I think I prefer, and what the documentation says (Application class documentation in the Flex 2.0.1 LiveDocs). To use the styleName property instead of styleName style, change the <mx:Script /> code block in the previous (big) example to the following:

<mx:Script>
    <![CDATA[
        private function plainStyle():void {
            Application.application.styleName = "plain";
        }

        private function defaultStyle():void {
            Application.application.styleName = undefined;
        }
    ]]>
</mx:Script>

Also, looking at the default.css style sheet in the Flex 3 beta SDK (see: [FlexSDK]\frameworks\projects\framework\defaults.css):

.plain
{
    backgroundColor: #FFFFFF;
    backgroundImage: "";
    horizontalAlign: "left";
    paddingBottom: 0;
    paddingLeft: 0;
    paddingRight: 0;
    paddingTop: 0;
}

{ 9 comments… read them below or add one }

1 John C. Bland II September 19, 2007 at 12:14 am

Does this work just the same but from the initial load?

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        horizontalAlign="center">

    …

</mx:Application>

Reply

2 John C. Bland II September 19, 2007 at 1:09 am

ok dude…I give up on pasting code. :-) Basically, can you set styleName=”plain” in the initial mx:Application tag?

Sorry for the multiple comments.

Reply

3 peterd September 19, 2007 at 7:05 am

Yeah, you can just do this if you want:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        horizontalAlign="center"
        styleName="plain">
    ...
</mx:Application>

Peter

PS: The trick to posting code is to change all your < characters to “[” or “&lt;”.

Reply

4 John C. Bland II September 21, 2007 at 12:55 am

Ok…I figured could just put it up there but I wasn’t sure since you were using setStyle().

Koo on posting code. I’ll keep that in mind.

Reply

5 Jesse December 19, 2007 at 7:00 am

Is there a way to change the style dynamically like this on just one component? Say I just wanted to change the style of button 1 but not the other two?

Reply

6 peterd December 19, 2007 at 7:54 am

Jesse,

Yes, you can apply a style to a single Button on control/container by setting the styleName property, such as:

<mx:Button label="Custom styled" styleName="myStyleName" />
<mx:Button label="Default styled" />

<mx:Style>
    .myStyleName {
        color: red;
    }
</mx:Style>

Hope that helps,
Peter

Reply

7 Paul November 2, 2009 at 7:09 pm

Is it possible to have conditionals inside styleName?

styleName="check1 || check2 ? 'style1' : 'style2'"

I can’t seem to make this approach work, I don’t see any compile errors but the styles are not being applied.

Thanks for your time.

-Paul

Reply

8 Peter deHaan November 3, 2009 at 6:53 am

@Paul,

Try using data binding:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
 
    <mx:Style>
        .style1 {
            color: red;
            fontSize: 32;
            fontWeight: bold;
        }
 
        .style2 {
            color: green;
            fontSize: 40;
            fontStyle: italic;
        }
    </mx:Style>
 
    <mx:ApplicationControlBar dock="true">
        <mx:CheckBox id="ch1" label="check1" />
        <mx:CheckBox id="ch2" label="check2" />
    </mx:ApplicationControlBar>
 
    <mx:Button label="Button" styleName="{(ch1.selected || ch2.selected) ? 'style1' : 'style2'}" />
 
</mx:Application>

Peter

Reply

9 Paul November 20, 2009 at 1:43 am

Hi Peter!

Thanks for the suggestion. Was not able to try it though, I just ended up using ActionScript to update the values like in your example.

Keep the great articles coming! :)

Reply

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: