Switch On The Code RSS Button - Click to Subscribe
Oct
9

Adobe AIR and Flex - Getting Started

So I decided it was time to start doing a few Adobe AIR application tutorials especially with the recent updates. This one is going to be a short tutorial on how to get your first AIR application going. The first thing we are going to do is get everything we need to start running and building air applications. This tutorial requires a small knowledge of Flex, if you need some tutorials on this we have plenty.

The first thing we are going to get is a trial for Flex Builder 3, right here. This will install the SDK and the proper stuff to get going. We are also going to install the runtime for AIR, this will enable you to be able to install AIR application using a .air file, grab this right here. Note though this tutorial is written in Beta 1. Now we are ready to get going.


You can install the application we will be building today by clicking the badge below (javascript needed) or downloading the air package. In the application you can do two simple things: if you click the load image button a file browser will pop up and then you can select an image off your computer, which will then be displayed in the application (and will resize when you resize the application). The second feature is a little cooler - you can drag and drop images onto the application and they will show up.


When you fire up Flex Builder and create your first AIR Project, just like we would create any other project, you will notice that we have another file in the root folder. In this case it is StartingAirProject-app.xml. This files holds some configuration information about our application. If we open up the file we see a bunch of comments and some xml tags. The important ones are explained in the file, basically though just make sure you set the appID in the opening application tag and a give it a name. My file looks like the following without the comments.

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://ns.adobe.com/air/application/1.0.M4"
  appId="com.pfp.tutorials.startingimage" version="1.0 Beta">

    <name>Getting Started with AIR</name>
   
    <title>Beginning with AIR</title>
   
    <description>
      First of many tutorials on using Adobe AIR
    </description>
   
    <copyright></copyright>   
   
    <rootContent systemChrome="standard" transparent="false"
      visible="true">

        [SWF reference is generated]
    </rootContent>
   
    <!--
    <icon>

        <image128x128>pfp_128.png</image128x128>
    </icon>
    -->
</application>

Now we are going to get started on the Flex code. Like most other tutorials, we start with a simplistic interface. This includes the root application, which you may notice is now a WindowedApplication, an image, and a button (both the image and button are nested inside a VBox with some padding). The WindowedApplication tag is the main AIR application tag, it has a few new properties - one, showStatusBar, we set to false because we do not need the bottom status bar. We end up with some starting interface code like below:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
  layout="absolute" width="324" height="320"
  showStatusBar="false">

  <mx:VBox height="100%" width="100%"
    paddingTop="5" paddingBottom="5"
    paddingLeft="5" paddingRight="5">

    <mx:Image id="img" x="0" y="0" width="100%" height="100%"
      verticalAlign="middle" horizontalAlign="center" />

    <mx:Button x="10" y="261" label="Load Image"/>
  </mx:VBox>
</mx:WindowedApplication>

To get an image loaded in we are going to add a click event to the button, which changes the button like so:

<mx:Button x="10" y="261" label="Load Image" click="loadImage()"/>

Next we can start writing the actionscript code to load the image. We start by adding a <Script> tag after the opening application tag and inside we import a couple of items, create a private variable for the image file, and write our loadImage function. This function first creates a FileFilter for the pop up browser to filter the items and then sets our image file equal to a new File. Then we can call browseForOpen on our file and lastly add an event listener for when the file has been selected. Now we need to write a function that gets called on the file select, which in my case is imageSelected(event:Event). All this function does is set the source of our image to the url of the file the user selected. This gives us the following code:

<mx:Script>
<![CDATA[
  import flash.desktop.DragManager;
  import flash.desktop.TransferableFormats;
  import flash.desktop.TransferableData;
 
  import flash.filesystem.File;
 
  private var imageFile:File;

  private function loadImage():void
  {
    var imagesFilter:FileFilter =
      new FileFilter("Images", "*.jpg;*.gif;*.png;*.swf;");
    imageFile = new File();
    imageFile.browseForOpen("Select an Image", [imagesFilter]);
    imageFile.addEventListener(Event.SELECT, imageSelected);
  }
 
  private function imageSelected(event:Event):void
  {
    img.source = imageFile.url;
  }
]]>
</mx:Script>

Now we can take a look at the more interesting feature of dragging an image onto our application. We start this by updating our application tag to call a function on creationComplete event. The update tag is below:

<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
  layout="absolute" width="324" height="320"
  showStatusBar="false" creationComplete="initApp()">

The actionscript function goes into the script tag we started above. Inside the function we add two event listeners, one for NativeDragEvent.NATIVE_DRAG_ENTER and one for NativeDragEvent.NATIVE_DRAG_DROP, these are two new events in AIR for handling drag and drop events from the operating system. We can then create the functions that are used for the events, dragEnter and draggedImage respectively. In the first we use the DragManager class to tell AIR that this application should accept items dragged onto it. In draggedImage we grab a reference to the files we dragged on (although here we really only care about the first one) and use its url to set the source of our image component. The new actionscript functions are below:

private function initApp():void
{
  addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER,
    dragEnter);
  addEventListener(NativeDragEvent.NATIVE_DRAG_DROP,
    draggedImage);
}

private function dragEnter(event:NativeDragEvent):void
{
  DragManager.acceptDragDrop(this);
}

private function draggedImage(event:NativeDragEvent):void
{
  var files:Array =
    event.transferable.dataForFormat(
      TransferableFormats.FILE_LIST_FORMAT
    ) as Array;
  img.source = files[0].url;
}

Yes that is really all that is needed to drag images onto our AIR application. Below is the final code for our main mxml file.

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
  layout="absolute" width="324" height="320"
  showStatusBar="false" creationComplete="initApp()">

  <mx:Script>
    <![CDATA[
      import flash.desktop.DragManager;
      import flash.desktop.TransferableFormats;
      import flash.desktop.TransferableData;
     
      import flash.filesystem.File;
     
      private var imageFile:File;
     
      private function initApp():void
      {
        addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER,
          dragEnter);
        addEventListener(NativeDragEvent.NATIVE_DRAG_DROP,
          draggedImage);
      }
     
      private function dragEnter(event:NativeDragEvent):void
      {
        DragManager.acceptDragDrop(this);
      }
     
      private function draggedImage(event:NativeDragEvent):void
      {
        var files:Array =
          event.transferable.dataForFormat(
            TransferableFormats.FILE_LIST_FORMAT
          ) as Array;
        img.source = files[0].url;
      }
   
      private function loadImage():void
      {
        var imagesFilter:FileFilter =
          new FileFilter("Images", "*.jpg;*.gif;*.png;*.swf;");
        imageFile = new File();
        imageFile.browseForOpen(
          "Select an Image",
          [imagesFilter]
        );
        imageFile.addEventListener(Event.SELECT, imageSelected);
      }
     
      private function imageSelected(event:Event):void
      {
        img.source = imageFile.url;
      }
    ]]>
  </mx:Script>

  <mx:VBox height="100%" width="100%"
    paddingTop="5" paddingBottom="5"
    paddingLeft="5" paddingRight="5">

    <mx:Image id="img" x="0" y="0" width="100%" height="100%"
      verticalAlign="middle" horizontalAlign="center" />

    <mx:Button x="10" y="261" label="Load Image"
      click="loadImage()"/>

  </mx:VBox>
</mx:WindowedApplication>

Only thing left to do is create the actual .air package so you can redistribute your awesome creations. In flex builder this is done very easily by going to the project menu then to "Export AIR Package..." and then you select what files should be used for the AIR package. Usually you shouldn't have to change any of the options (except maybe the destination filename).

With that step finished we now have gone from knowing little to nothing about creating AIR applications to exporting our first AIR package. The only thing left to do is install your application. Now remember, this is only the beginning of what can be done with Adobe AIR - check back soon for some more cool tutorials on Adobe AIR and Flex. If you have any questions don't hesitate to leave a comment.



Posted in Adobe AIR, Flex, All Tutorials by The Fattest |

6 Responses

  1. TG Says:

    Hello,

    //changed for Flex Builder 3
    //Version: 3.0.190133

    import flash.desktop.Clipboard;

    var files:Array =
    event.clipboard.getData(
    ClipboardFormats.FILE_LIST_FORMAT
    ) as Array;
    img.source = files[0].url;

  2. The Fattest Says:

    TG, thanks for pointing that out. I am swamped right now and don’t have the time to play around with the new beta too much. But once I get some time I am going to update all the tutorials.

  3. Raja Junaid Says:

    Dear Fattest,

    Kindly tell me where I have to place the PHP file. Basically, I have my website in PHP. Now I want to make desktop application in ADOBE AIR. But I am stuck in making a login page.

    Kindly tell me how can I connect through my existing web database using ADOBE AIR.

    Thanks in advance.

  4. Jixx Says:

    this line:

    DragManager.acceptDragDrop(this);

    is giving me this error:

    1120: Access of undefined property DragManager.

    any idea whats up?

  5. james Says:

    Interesting flex tutorials,Found more links on air @ www.adobeairtutorials.com

  6. Mike Says:

    I got this Air application to work in Flex 3 with a few quick changes:

    1. import flash.desktop.DragManager; to
    import flash.desktop.NativeDragManager;

    2. DragManager.acceptDragDrop(this); to
    NativeDragManager.acceptDragDrop(this);

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Powered by WP Hashcash