19 June 2011
For a quick mobile tutorial that doesn't require you to set up a server, try the Twitter Trends tutorial.
FlexMobileTestDrive.mxml
<?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.FlexMobileTestDriveHomeView"
applicationDPI="160">
</s:ViewNavigatorApplication>
FlexMobileTestDriveHomeView.mxml
<?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="HomeView">
<s:titleContent>
<s:TextInput id="searchTxt" prompt="Employee Name" width="100%"/>
</s:titleContent>
<s:actionContent>
<s:Button id="searchBtn" >
<s:icon>
<s:MultiDPIBitmapSource source160dpi="@Embed('assets/search160.png')"
source240dpi="@Embed('assets/search240.png')"
source320dpi="@Embed('assets/search320.png')"/>
</s:icon>
</s:Button>
<s:Button id="addBtn">
<s:icon>
<s:MultiDPIBitmapSource source160dpi="@Embed('assets/add160.png')"
source240dpi="@Embed('assets/add240.png')"
source320dpi="@Embed('assets/add320.png')"/>
</s:icon>
</s:Button>
</s:actionContent>
<s:List id="empList" left="0" right="0" top="0" bottom="0">
</s:List>
</s:View>
In this Test Drive, you are going to create a Flex mobile application that retrieves, displays, and modifies database records (see Figure 1). A Flex application does not connect directly to a remote database. Instead, you connect it to a data service written in your favorite web language (PHP, ColdFusion, Java, or any other server-side technology). You will build the front-end Flex mobile application; the database and the server-side code to manipulate database records is provided for you as a PHP class, a ColdFusion component, or Java classes.
In this module, you are going to build a Flex mobile application that retrieves data from a database and displays it. A list of employees will be displayed in one application view and employee details in another. First, you build and test the application using a desktop emulator. Next, you run and test the application on an attached mobile device.
In this tutorial, you create a new Flex Mobile project and build the user interface. You'll retrieve data from the server and display it in the next tutorial.
Follow the instructions in one of the sections below.
The setup files include a database and server-side files to manipulate data in the database. Your Flex application will call methods of one of these server-side files, EmployeeService.
Note: If you installed the server files for the Flex 4.5 Test Drive or the Flex 4.5 trial email sample applications, you can skip this step. The same server-side files are used for all three.
username, password, server, and databasename properties to the correct values for your server setup. Test the setup by browsing to http://{your server:your port}/TestDrive/test/test.html. For example, browse to:http://localhost/TestDrive/test/test.html
You should see a list of the names of the employees in the database successfully displayed.
username, password, server, port, and databasename properties to the correct values for your server setup. This class file contains the methods you will call from your Flex application.<property-case>
<!-- cfc property names -->
<force-cfc-lowercase>true</force-cfc-lowercase>
<!-- Query column names -->
<force-query-lowercase>true</force-query-lowercase>
<!-- struct keys -->
<force-struct-lowercase>true</force-struct-lowercase>
</property-case>
Note: If you are using an earlier version of ColdFusion, your configuration file may not have these tags and you will need to add them. See the documentation on using Flash Remoting with your particular server.
Note: For Windows, the ConnectionHelper.java file assumes the testdrive web app is deployed on the C drive. If it is not deployed on the C drive, you need to modify this file appropriately and recompile it, replacing /{your server webapps folder}/testdrive/WEB-INF/classes/services/ConnectionHelper.class.
Follow the instructions in one of the sections below.
id to com.adobe.samples.FlexMobileTestDrive (see Figure 5). The application ID is used by the AIR runtime and the operating system to identify the application. In order to guarantee a unique application ID, a common naming convention is to use reverse domain name notation as used here. You will modify additional attributes in this file in a later tutorial.
Use the Package Explorer to explore the generated project(s) and files.
When you create a new Flex Mobile project, Flash Builder creates three or four files in the Flex project: two MXML files and one or two XML files (see Figure 11). You create Flex applications using two languages: ActionScript and MXML. Typically, you use MXML and Flex components to lay out application interfaces and ActionScript to program application logic. MXML tags and ActionScript code can reference each other, similar to HTML tags and JavaScript code. When you compile an application, the MXML is parsed and converted to ActionScript in memory, and then the ActionScript is compiled into bytecode. The result, a SWF file, is rendered by Flash Player for web applications and the Adobe AIR runtime for mobile and desktop applications and mobile applications on Google Android devices and Blackberry Playbooks. iOS devices do not support AIR so when packaging an application for the iOS platform, the SWF is converted to a native iOS application.
FlexMobileTestDrive.mxml is the main application file. The first line of code is the XML declaration for the parser. The next line is the ViewNavigatorApplication tag, which is the root container for a view-based Flex mobile application. It has a firstView attribute set to views.FlexMobileTestDriveHomeView (the second MXML file automatically generated). This is the view that displays on the device when you launch the application. Unlike desktop or web applications built in Flex, you typically do not put much content in the application container. For mobile applications, most content is placed in individual views. The ViewNavigatorApplication tag also has an applicationDPI attribute set to 160, the target device density.
The third file created, FlexMobileTestDrive-app.xml, is the application descriptor file. It includes info about the application, including its name, id, associated icons, what the initial window should look like, as well as Android and iOS specific settings. If you selected Blackberry Tablet OS as a target platform for the application, you will have a fourth file, blackberry-tablet.xml, in which you set Blackberry Tablet OS specific settings.
Download and unzip testdrivemobile_assets.zip and add the assets folder to your project (see Figure 12).
The assets folder contains graphics that you will use in the application.
Switch to Design mode for FlexMobileTestDriveHomeView.mxml and from the Components view, drag out a TextInput and place it in the title area (titleContent) of the ActionBar (see Figure 13). Use the Properties view to assign an id of searchTxt, a prompt of Employee Name, and a width of 100% (see Figure 15).
Flex applications are built from components such as Buttons, CheckBoxes, and Lists. The Flex 4 framework includes over 100 components, including user interface controls to display content and provide user interaction, and containers to manage the layout. The Flex 4.5 framework includes components and component skins that are optimized for mobile use and in some cases, enhanced to enable touch and throw scrolling. It also contains several new components specifically designed for mobile development including ViewNavigatorApplication, TabbedViewNavigatorApplication, View, ViewNavigator, TabbedViewNavigator, and ActionBar. When you create a mobile project, the Flash Builder Component view displays a subset of Flex components reccomended for mobile use.
The root container of your application, the ViewNavigatorApplication container, has a single child, a ViewNavigator. The ViewNavigator manages an ActionBar and a set of View instances using a stack-based history mechanism. The ActionBar component is displayed at the top of the application and appears above the views and consists of three areas: a navigator area, a title area, and an action area (see Figure 14).
Typically, you place components that let the user navigate the content (like back or home buttons) in the navigator area, a string or other components (like the TextInput used in this application) in the title area, and components that let users take an action (like buttons to search or add employees in this application) in the action area.
For each component, you can set properties and styles. Properties apply only to that particular component instance. Styles can be set inline or using CSS to create style rules to apply to your components. You can view and set object properties, styles, and more using the Flash Builder Properties view (see Figure 15).
Switch to Source mode and take a look at the generated code.
In Design mode, set the preview device to Motorola Droid Pro (see Figure 16). Using the Components view, add two Button instances to the action area (actionContent) of the ActionBar (see Figure 16). Assign the first Button an id of searchBtn, delete the label, and and click the folder next to icon and select Multi-Resolution Bitmap (see Figure 17). Embed the bitmaps search160.png, search240.png, and search320.png located in the assets folder (see Figure 18). Assign the second button an id of addBtn, delete the label, and set its icon property to a multiresolution bitmap with embedded images add160.png, add240.png, add320.png.
When you created the project, you set the application DPI to 160. When using Design mode to build the application interface, you should set the preview device to one that has this same density so the design view pixels will correspond one-to-one with the pixels in the Flex code. The Motorola Droid Pro is a 160 DPI device. You will look up device DPIs in a later step.
Using the Components view, drop a List control below the ActionBar. Assign an id of empList and set constraints so that it fills the entire view area (see Figure 19).
The layout of a container determines how its children are positioned within it. Some containers have a preset layout mechanism (like the VGroup and HGroup containers). For others, like the View container, you can set the layout yourself. For example, you could specify a vertical layout, a horizontal layout, a basic layout , or more. Basic layout uses absolute positioning in which the positions of objects are set using x and y values (the upper-left corner of the container is 0,0) or constraints. Constraints allow you to "pin" an object to a specific location in a container, providing a way to build flexible interfaces. You set constraint properties (top, bottom, left, right, verticalCenter, horizontalCenter) to the number of pixels the object should be from that part of the container.
Switch to Source mode and use Flash Builder Content Assist to select and set values for various properties and styles.
When you place your cursor inside a tag and press the spacebar or Ctrl+Spacebar, you get code hinting with the Flash Builder Content Assist (see Figure 20). It shows a list of all the attributes you can set for that tag, including properties, styles, events, and more. Different symbols are used for each different type of attribute. This is the same list you see in the Alphabetical view of the Design mode Properties view.
Select Help > Dynamic Help and then click a component tag in your code. In the Help view, click the component's API link to view its API (see Figure 21).
The Adobe Help AIR application opens, which allows you to view help content (including the ActionScript Language Reference, ASDocs) when you are online or offline (once the content has been downloaded and saved locally). A component's API, its application programming interface, lists everything you can set for that component and includes descriptions and code examples. If you do not see the class explorer on the left side, click the Show Packages and Class List link in the upper-left corner of the window.
Use the Run button or the Run menu to compile the application. For PHP developers using Flash Builder for PHP, you can select to Run As a Mobile (PHP) Application or a Mobile Application. In the Run Configurations dialog box, launch the application on the desktop simulating a specific mobile device for a specific target platform (see Figure 22). Use the Device menu to rotate the application (see Figure 24). View common device configurations in Flash Builder by selecting Run > Run Configurations and then clicking the Configure button (see Figure 25). Then, create two run configurations for devices with different pixel densities and run the application using each (see Figures 23 and 26).
PHP developers using Flash Builder for PHP: The first time you run an application, you get a Run As dialog box. If you are running the application there is no difference between the two. If you are debugging the application and also want to debug any of the PHP code (covered in a later tutorial), you need to debug as a Mobile (PHP) Application for the PHP Debugger to run.
Your application runs in the AIR Debug Launcher (ADL) (see Figure 23). The ADL provides a way for you to run and debug mobile and desktop applications without having to first install them on a computer or device.
The ADL also provides menu items in the Device menu to emulate devices and device actions, such as rotating the device or pressing the Back button (see Figure 19). Simulate rotating the device and see the ActionBar automatically resizes to fit the screen. Note that in the emulation the view looks somewhat large because the pixel density of mobile devices is higher than that of desktop monitors.
Next, simulate the application on a device with a different pixel density. Return to Flash Builder, select Run > Run Configurations, and then click the Configure button in the launch method area. You should see a table listing screen sizes and densities for common mobile devices (see Figure 25). You can also modify this list to add your own device configurations. Notice the Motorola Droid Pro has a lower PPI and will thus use the 160 dpi assets you specified. The other Motorola Droids will use the 240 dpi assets.
Return to the run configuration dialog box and rename the existing configuration to specify a dpi or device. For example, if it is using the Motorola Droid Pro device, call it FlexMobileTestDrive (160) or FlexMobileTestDrive (Droid Pro). Click the Duplicate Launch Configuration button in the upper-left corner and create a second launch configuration for a device with a different pixel density, for example FlexMobileTestDrive (240) for the Motorola Droid X. Run the application. You will see a different size window with an application scaled to the appropriate size and using the appropriate multi-resolution assets (see Figure 26).
You have now created your application interface and tested it using the ADL simulator. In the next tutorial, you retrieve data from the database and display a list of employees in the List control.
Refer to the following resources to learn more about this topic:
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.