Book Image

Flash Development for Android Cookbook

By : Joseph Labrecque
Book Image

Flash Development for Android Cookbook

By: Joseph Labrecque

Overview of this book

Flash has now arrived to Android — the fastest growing smartphone platform. This offers massive opportunities for Flash developers who want to get into mobile development. At the same time, working on smartphones will introduce new challenges and issues that Flash developers may not be familiar with. The Flash Development for Android Cookbook enables Flash developers to branch out into Android mobile applications through a set of essential, easily demonstrable recipes. It takes you through the entire development workflow: from setting up a local development environment, to developing and testing your application, to compiling for distribution to the ever-growing Android Market. The Flash Development for Android Cookbook starts off with recipes that cover development environment configuration as well as mobile project creation and conversion. It then moves on to exciting topics such as the use of touch and gestures, responding to device movement in 3D space, working with multimedia, and handling application layout. Essential tasks such as tapping into native processes and manipulating the file system are also covered. We then move on to some cool advanced stuff such as Android-specific device permissions, application debugging and optimization techniques, and the packaging and distribution options available on the mobile Android platform. In a nutshell, this cookbook enables you to get quickly up to speed with mobile Android development using the Flash Platform in ways that are meaningful and immediately applicable to the rapidly growing area of mobile application development.
Table of Contents (18 chapters)
Flash Development for Android Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface

Converting a standard Flex project to a Flex Mobile project


There is currently no workflow within Flash Builder (or FDT) to convert an existing application to a mobile Android application. Depending upon the complexity of the application being converted and the version of Flex, it may be undergoing conversion from this task can range from the very simple to one that is inordinately complex. In this recipe, we will demonstrate a simpler example using basic Flex structures.

How to do it…

Create a new mobile project and copy all of the necessary files into it, retaining those portions of code which are used for mobile projects and modifying any unsupported components.

For this example, we'll use a simple Flex project targeting AIR for desktop consisting of nothing but a button component at this stage:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<s:Button x="10" y="10" width="300" height="200" label="Button"/>
</s:WindowedApplication>

To convert this to a new Flex Mobile project, take the following steps:

  1. 1. Go to the menu and choose File | New | Flex Mobile Project.

  2. 2. Provide the project setup dialog with information about the new mobile project.

    Note

    The project cannot have the same name as any existing project within your environment.

  3. 3. Copy all of your files from the project folder in your original project into this new mobile project excluding your project descriptor file ({myApp }.xml) and Default Application files.

  4. 4. Now, copy everything within your old Default Application file and paste it into the Default Application file that was created along with your mobile project. Once everything has been copied over, right-click on the main application file and choose Set as Default Application.

  5. 5. Change all instances of <s:WindowedApplication> to <s:ViewNavigatorApplication> (alternatively, <s:TabbedViewNavigatorApplication>).

    Note

    Just as with a standard AIR <s:WindowedApplication>, only one instance of <s:ViewNavigatorApplication> or <s:TabbedViewNavigatorApplication> can exist within a project.

  6. 6. Look within your Problems panel to see whether or not any further modifications need to be made.

  7. 7. If you are not using any of the old Halo components (mx namespace) it is a good idea to remove the namespace declaration for your opening <s:ViewNavigatorApplication> tag.

  8. 8. Add a firstView attribute to the <s:ViewNavigatorApplication> tag. This should point to the View automatically created when you set up the mobile project.

  9. 9. Since visual UI elements cannot reside directly within a <s:ViewNavigatorApplication /> node, we must wrap the <s:Button /> instance within a <fx:Declarations> </fx:Declarations> tag set, or move it to a specific View.

Note

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Your Default Application file should now read as follows:

<?xml version="1.0" encoding="utf-8"?>
<s:ViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" firstView="views.MobileFlexProjectHomeView">
<fx:Declarations>
<s:Button x="10" y="10" width="447" height="106" label="Button"/>
</fx:Declarations>
</s:ViewNavigatorApplication>

Additionally, a view for this application could appear as such:

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="MobileFlexProjectHomeView ">
</s:View>

For more information about how Flex Mobile projects are structured, have a look at the following resource: http://opensource.adobe.com/wiki/display/flexsdk/Developer+Documentation.

How it works…

When using Flex, the root tag of your application determines largely what APIs and structures are available to you throughout the project. Making sure that we choose the correct root tag is very important in regard to the target platform and capabilities of our project. For AIR on Android, we will want to use either ViewNavigatorApplication or TabbedViewNavigatorApplication. Desktop applications would use the Application or WindowedApplication tags. Chances are, if you are building Flash content with Flex that is to be deployed to Flash Player in the browser, on both mobile and desktop you will use a straight Application tag for your project.

There's more…

If you don't want to deal with a lot of conversion, and are just starting out with a new project that will share the same codebase across desktop and mobile, you might consider using a Flex Library project to allow different projects to share the same underlying codebase.

Read the documentation on Flex 4 Library usage at: http://help.adobe.com/en_US/flashbuilder/using/WS6f97d7caa66ef6eb1e63e3d11b6c4d0d21-7fe6.html.