Tam
Tam

Reputation: 12042

How to detect Screen width/height change in Flex

I have objects placed on the screen using x/y coordinates. I want a way to update that based on changes in Screen Width/Height. So if used re-sized browser window x/y should change. How do I cick off a function every time the screen is re-sized.

Upvotes: 2

Views: 10518

Answers (3)

FlexRipper
FlexRipper

Reputation: 11

The Best Way to detect changes in screen resolution is by simply Adding a event listener on application level and use the event properties to detect changes


private function init():void
{
    this.addEventListener(ResizeEvent.RESIZE, resizeApplication);
}

public function resizeApplication(event:ResizeEvent):void
{
     if(this.stage.stageHeight != event.oldHeight)
     {
        //perform action here or just use the above properties in any way you want
     }

}

I made this function public so that it can be used anywhere in the application in case the event got fired but the view attached to it was not listening.

Upvotes: 1

Jason
Jason

Reputation: 2503

You can setup a listener to the Resize event on the Application tag right in the mxml.

Although you may want to explore percentage based setting instead, it would be less of a effort in most cases.

Upvotes: 1

wajiw
wajiw

Reputation: 12269

You can do that by attaching an ResizeEvent listener to whatever object you want to track:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/events/ResizeEvent.html

public class Main extends Sprite {
    public function Main (  ) {

      stage.addEventListener(Event.RESIZE, resizeListener);
    }

    private function resizeListener (e:Event):void {
      trace("The application window changed size!");
      trace("New width:  " + stage.stageWidth);
      trace("New height: " + stage.stageHeight);
    }
  }

Upvotes: 4

Related Questions