Flex Spinning Progress Indicator Component

Sometimes you need to indicate progress to users when the application is doing something, like calling a remote web-service or something. At times, it can be nice to incorporate this as an element in the UI, that operates in an unobtrusive way. For instance showing a small indicator on or besides the button, that initiated the operation in progress.

Here is a small spinning wheel example on how to do this with an old style ASCII spinning wheel animation. Enter the LabelProgressIndicator.mxml component:

<?xml version="1.0" ?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Script><![CDATA[
        [Bindable]
        private var statusText:String;

        [Bindable]
        private var progressText:String;

        private const SPIN_CHARS:String = "/-\\|/-\\|";

        private var workingDotsTimer:Timer;
        private var progressCounter:int;

        public function startProgress(status:String):void {
            if (workingDotsTimer == null) {
                statusText = status;
                workingDotsTimer = new Timer(200);
                progressCounter = 0;
                workingDotsTimer.addEventListener(TimerEvent.TIMER, onWorkingDotsTimer);
                workingDotsTimer.start();
            }
        }

        public function stopProgress():void {
            if (workingDotsTimer != null) {
                workingDotsTimer.removeEventListener(TimerEvent.TIMER, onWorkingDotsTimer);
                workingDotsTimer.stop();
                workingDotsTimer = null;
                statusText = null;
                progressText = null;
            }
        }

        private function onWorkingDotsTimer(event:TimerEvent):void {
            if (progressCounter == SPIN_CHARS.length - 1) {
                progressCounter = 0;
            } else {
                progressCounter++;
            }
            progressText = SPIN_CHARS.charAt(progressCounter);
        }

        ]]></mx:Script>

    <mx:Label id="status" text="{statusText}"/>
    <mx:Label id="progress" text="{progressText}" fontFamily="courier" fontWeight="bold"/>
</mx:HBox>

and here is how to use it:

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:ui="*">

    <mx:Button click="progress.startProgress('working')" label="Start"/>
    <mx:Button click="progress.stopProgress()" label="Stop"/>

    <ui:LabelProgressIndicator id="progress" />

</mx:Application>

October 28, 2009  Tags:   Posted in: Programming, Rich Internet Applications

6 Responses

  1. James Ward - October 28, 2009

    I get the following error when I try to run the demo:
    Error #2032: Stream Error. URL: http://www.techper.net/blog/wp-content/uploads/2009/10/textLayout_4.0.0.10485.swz

    -James

  2. Erich Cervantez - October 28, 2009

    If you’re looking for a spinning graphic that’s freely available and quite popular, check out a former co-worker (Jake Hawkes) of mine’s example here on the Adobe Exchange:

    http://www.adobe.com/cfusion/exchange/index.cfm?event=extensionDetail&loc=en_us&extid=1219018

  3. polesen - October 28, 2009

    Hi James,

    Strange. It worked fine for me in FF on Mac with player 10. I then tried in Safari, and it crashed. Ouch! I had built that swf with flex 4.0.0.10485.

    I just uploaded one built with flex3 instead, and targeted the player in v9.

    Does that work better for you? (I know my Safari doesn’t crash on it anymore :-) )

  4. polesen - October 28, 2009

    @Erich: Cool, thank you for the link!

  5. APintex - October 30, 2009

    Return to MSDOS time?… :)

  6. polesen - October 30, 2009

    @APintex: Very much so, yes :-)

Leave a Reply