Build Your First Mobile Flex Application: Lab Exercises
Build Your First Mobile Flex Application: Lab Exercises
Build Your First Mobile Flex Application: Lab Exercises
Table
of
Contents
General
tips .................................................................................................................3
Overview
of
the
application .........................................................................................4
Exercise
1:
Creating
the
project
and
setting
up
the
Action
Bar......................................5
Exercise
2.
Lay
out
the
Trends
view..............................................................................8
Exercise
3.
Connect
the
trends
view
to
data ...............................................................10
Exercise
4.
Debug
the
application
on
the
desktop ......................................................12
Exercise
5.
Add
a
view
to
display
tweets
for
a
trend...................................................14
Exercise
6.
Set
up
view
navigation..............................................................................17
Exercise
7.
Configure
the
list
item
renderer................................................................19
Exercise
8.
Debug
the
application
on
a
device ............................................................22
Exercise
9.
Add
a
view
to
show
information
for
a
selected
user .................................26
Exercise
10.
Set
up
data
persistence
between
sessions ..............................................32
Exercise
11.
Export
an
application
package
for
release...............................................34
Appendix
1.
Installing
the
USB
driver
on
Windows.....................................................36
General
tips
Welcome
to
the
preview
release
of
the
new
mobile
development
features
in
Flash
Builder
"Burrito"
and
Flex
SDK
"Hero"!
Because
this
is
a
preview
release,
there
are
known
bugs,
performance
problems,
usability
issues,
and
missing
features.
Items
in
boxes
are
optional
information
that
you
can
read
if
you
want
to
understand
more
of
what
youre
doing.
Its
not
necessary
to
read
this
text
to
follow
the
steps.
This
tutorial
relies
on
a
set
of
assets
provided
as
TwitterTrendsAssets.zip.
If
you
don't
have
these
assets,
you
can
download
them
from
the
Tutorials
and
Samples
page
on
Adobe
Labs
at
http://labs.adobe.com/technologies/flex/samples.
These
exercises
are
written
to
be
followed
in
order.
However,
weve
provided
FXP
project
files
in
the
TwitterTrendsAssets
zip
for
the
start
of
each
exercise,
so
if
you
get
stuck
or
lost
in
the
middle
of
one
of
the
exercises,
you
can
restart
from
the
beginning
of
the
exercise,
or
skip
ahead
to
the
beginning
of
the
next
exercise.
Use
the
FXPs
in
staticCheckpoints
if
you're
using
static
data,
or
liveCheckpoints
for
live
data.
(See
the
note
in
Exercise
3
for
information
on
static
vs.
live
data.)
To
import
an
FXP
file:
1. (Optional)
Delete
your
existing
TwitterTrends
project
by
right-/control- clicking
on
the
root
of
the
project
in
the
Package
Explorer
and
choosing
Delete.
This
avoids
confusion
with
the
new
version
you're
going
to
import.
2. Choose
File
>
Import
3. Expand
the
Flash
Builder
folder
and
select
Flash
Builder
Project.
4. Click
Next
>.
5. Click
the
Browse
button
to
the
right
of
the
File
field,
browse
to
the
FXP,
and
click
OK.
6. Click
Finish.
If
all
else
fails,
dont
be
afraid
to
ask
the
teachers
or
a
TA
for
help!
Or,
if
youre
doing
these
exercises
outside
of
the
MAX
lab,
post
a
question
to
the
Flash
Builder
forum
or
the
Flex
SDK
forum.
By default, mobile Flex applications are structured as a series of viewsindividual screens of UI the user navigates between. Above the views sits the Action Bar, which typically contains the title of the current view and one or more controls for navigation, search, or other actions. In the example above, the first view, the Trends view, consists of a single Flex List component. In mobile applications, the List component scrolls in response to touch input. The Action Bar for this view shows the title of the view, as well as a Refresh button. The other views are structured similarly, though in the third view, the User Info view, the Action Bar has been customized to show the picture of the selected user.
6 | Build Your First Mobile Flex Application Flash Builder generates two MXML components: TwitterTrends.mxml, which is the main application file, and TwitterTrendsHome.mxml, which is the first view of your application and is based on View. Note that unlike in desktop Flex, you don't typically put much UI content in the main applicationmost of your UI goes into the individual views. The one exception is the ActionBar, as we'll see next.
Add
global
content
to
the
Action
Bar
in
the
main
application
file
The
Action
Bar
is
a
special
component
in
mobile
Flex
applications.
It's
part
of
the
overall
application,
not
part
of
each
individual
view.
You
can
add
items
to
it
either
in
the
main
MobileApplication
or
in
an
individual
view.
In
this
application,
you'll
add
a
Refresh
button
that's
available
in
all
views.
In
Exercise
9,
we'll
show
you
how
to
customize
the
ActionBar
in
a
specific
view.
1. In
the
editor,
switch
to
your
main
application
file,
TwitterTrends.mxml.
2. Delete
the
empty
<fx:Declarations> </fx:Declarations>
tag.
3. Add
the
following
code
between
the
<s:MobileApplication>
and
</s:MobileApplication>
tags:
<s:actionContent> <s:Button id="refreshBtn" icon="@Embed(source='assets/refresh48x48.png')" click="Object(navigator.activeView).refresh()"/> </s:actionContent>
The Action Bar has three content areas: navigationContent on the left, titleContent in the middle, and actionContent on the right. By default, the title area of the Action Bar contains a text component that is bound to the title of each view, so you do not need to specify it. In this tutorial, we only set the Action Bar content in the main application, but you can set the content for any of the content areas in individual views as well in order to show content specific to that view. Setting one of the content areas in an individual view overrides the content from the application. Since we're adding the Refresh button into the global content for the Action Bar, the action it needs to take depends on whichever view is currently visible. So the click handler gets the currently active view and calls a
Build Your First Mobile Flex Application | 7 refresh() method on it. You'll implement this method in each of the views you'll be creating in this tutorial.
3. 4. 5. 6. 7. 8.
3. From the Device dropdown, choose a device with a different screen sizefor example, the Motorola Droid. The view should get larger horizontally and everything should resize properly. 4. Click the Portrait orientation button and double-click on the editor tab again to restore the panels.
Static
data
1. Choose
Data
>
Connect
to
XML
2. For
the
Path
field,
click
Browse
and
browse
to
the
trends.xml
file
in
the
src/sampledata
folder.
3. Set
the
service
name
to
TrendsService.
Leave
all
other
fields
as
the
default
and
click
Finish.
4. In
the
Data/Services
panel
at
the
bottom,
bring
up
the
context
menu
for
getData()
and
choose
Generate
Service
Call.
This
switches
you
to
Source
view
and
shows
that
a
new
getData()
function
was
generated.
5. Rename
the
new
getData()
function
in
the
code
to
getTrends().
(The
function
will
still
call
getData()
on
the
service.)
Live
data
1. Choose
Data
>
Connect
to
HTTP
2. Change
the
existing
name
"Operation1"
to
getTrends
3. Change
the
URL
of
the
operation
to
http://search.twitter.com/trends.json
4. In
the
Service
name
field,
enter
TwitterService
and
click
Finish.
5. In
the
Data/Services
panel
at
the
bottom,
bring
up
the
context
menu
for
getTrends()
and
choose
Configure
Return
Type
6. Make
sure
Auto-detect
is
selected
and
click
Next.
7. There
are
no
parameters
to
configure.
Click
Next
again.
8. Enter
Trends
for
the
name
of
the
type.
9. To
avoid
confusion,
change
the
type
of
the
trends
property
to
Trend[]
(not
Trends[]).
This
makes
it
so
each
trend
object
is
called
"Trend"
rather
than
"Trends".
10. Click
Finish.
11. From
the
context
menu
for
getTrends(),
choose
Generate
Service
Call.
This
switches
you
to
Source
view
and
shows
that
a
new
getTrends()
function
was
generated.
Build Your First Mobile Flex Application | 11 In the code, you'll see that two items were added to the <Declarations> tag: a service object (either TwitterService or TrendsService, depending on which data you're using) and a CallResponder. The service object has methods to initiate network calls to the service. The CallResponder watches the service calls, and when they complete, sends out events to other components notifying them of the data returned by the service call. When working with static data, each XML file is accessed by a separate service with a single getData() method that returns the data. When working with HTTP services, you can choose to create multiple methods for different URLs on the same service.
Call
the
service
when
the
user
navigates
to
or
refreshes
the
view
Recall
that
in
the
main
TwitterTrends.mxml
application
file,
the
refresh
button
in
the
Action
Bar
was
set
to
call
a
refresh()
method
on
each
view.
You'll
implement
this
method
in
TwitterTrendsHome.mxml
to
call
the
appropriate
service
call.
You'll
also
call
this
method
from
the
view's
viewActivate
event,
which
is
called
whenever
the
user
navigates
forward
or
back
to
the
view.
1. Inside
the
<fx:Script>
block,
add
the
following
method:
public function refresh(): void { getTrends(); }
2. Add
viewActivate="refresh()"
to
the
<s:View>
tag
at
the
top
of
the
file.
Set
a
breakpoint
1. Switch
back
to
Flash
Builder.
2. In
TwitterTrendsHome.mxml,
locate
the
refresh()
method.
3. Double-click
in
the
gray
gutter
to
the
left
of
the
call
to
getData()
or
getTrends()
in
the
refresh()
method.
A
blue
dot
appears
in
the
gutter
representing
your
breakpoint.
Build Your First Mobile Flex Application | 13 4. Click the refresh icon in the Action Bar. Flash Builder shows a dialog asking if you want to switch to the Flash Debug perspective. Check Remember my decision and click Yes. 5. In the Flash Debug perspective, new panels appear. The Debug panel shows the current call stack. The top of the stack is the refresh() method you stopped in. Clicking on other calls in the call stack navigates to other methods. The Variables panel shows the values of variables in the current scope as well as in the current object (this). Click the triangle next to this to see member variables and their values. The Breakpoints panel shows the breakpoints you currently have set. The Expressions panel lets you create specific expressions whose values you want to monitor whenever you're stopped. 6. In the Debug panel, click the Step into button several times. Flash Builder will step into the getData() or getTrends() method. 7. Click the Resume button and switch back to the ADL window. Your application continues running. 8. Quit ADL. 9. In the upper-right-hand corner of Flash Builder, click on the Flash button to switch back to the Flash Development perspective. You may need to resize the tab containing the perspective buttons in order to see it. You can also choose Window > Open Perspective > Flash.
Static
data
1. Choose
Data
>
Connect
to
XML
2. For
the
Path
field,
click
Browse
and
browse
to
the
search.xml
file
in
the
src/sampledata
folder.
3. Set
the
service
name
to
TweetsService.
Leave
all
other
fields
as
the
default
and
click
Finish.
4. In
the
Data/Services
panel
at
the
bottom,
bring
up
the
context
menu
for
getData()
under
TweetsService
(not
the
original
TrendsService)
and
choose
Generate
Service
Call.
This
switches
you
to
Source
view
and
shows
that
a
new
getData()
function
was
generated.
5. Rename
the
getData()
function
to
getTweets().
Live
data
1. In
the
Data/Services
panel,
bring
up
the
context
menu
for
TwitterService
and
choose
Properties.
2. Click
the
Add
button
above
the
Operations
table.
3. Change
the
existing
name
"Operation1"
to
getTweets
4. Change
the
URL
of
the
operation
to
http://search.twitter.com/search.json
5. Click
the
Add
button
above
the
Parameters
table
twice
to
add
two
parameters.
6. Change
the
parameter
names
to
q
and
result_type.
Make
sure
to
press
the
Return
key
after
each
name.
7. Click
Finish.
8. In
the
Data/Services
panel,
bring
up
the
context
menu
for
getTweets()
and
choose
Configure
Return
Type
9. Make
sure
Auto-detect
is
selected
and
click
Next.
10. In
the
Enter
Value
column,
set
the
q
parameter
to
Flex
and
the
result_type
parameter
to
recent.
11. Click
Next.
12. Enter
Tweets
for
the
name
of
the
type.
13. In
the
results
property,
change
the
type
name
from
Results[]
to
Tweet[].
14. Click
Finish.
15. From
the
context
menu
for
getTweets(),
choose
Generate
Service
Call.
This
switches
you
to
Source
view
and
shows
that
a
new
getTweets()
function
was
generated.
Call
the
service
when
the
user
navigates
to
or
refreshes
the
view
As
in
the
trends
view,
you'll
do
this
by
implementing
the
refresh()
method.
1. Inside
the
<fx:Script>
block,
add
the
following
method:
Static
data
public function refresh(): void { getTweets(); }
Live
data
public function refresh(): void { getTweets(String(data), "recent"); }
2. Add viewActivate="refresh()" to the <s:View> tag at the top of the In a real application going against live data, you pass the data property of the TweetsView into the Twitter search service. In the next exercise, we'll see how this data property is passed into the TweetsView. In the static data case, there's no real backend to query from, so you don't pass any parametersyou just always get the same fake data.
18 | Build Your First Mobile Flex Application navigator.pushView(TweetsView, list.selectedItem.name); This tells the ViewNavigator to create an instance of TweetsView, and to pass it the name of the selected trend as its data property. In the previous exercise, you bound the data property into the title of the TweetsView and (if you're using the live API) used it to query the Twitter search service for tweets relevant to the trend. If you run the application now and tap on a trend in the initial view, you'll see that the tweets are shown in the list. However, you only see one line of each tweet, and you don't see the image or name of the person who posted the tweet. You'll configure this in the next exercise.
The field properties specify which members of each data item to use as the message and icon. By using messageField instead of labelField for the text, you enable the text to wrap to multiple lines. labelField (which maps to the headerField of the item renderer) displays in bold and does not wrap by default. Note that in this preview release of "Hero", the item renderer does not automatically grow vertically if the tweet gets taller, which is why we put a fixed height on it. In the final release, the height will not be necessary since the renderer will grow to accommodate its content. 7. Click the Run button in the Flash Builder toolbar to run the application on the desktop. 8. Click on a trend in the initial view. (If using static data, click on "Inception".) This brings you to the tweets view for that trend. 9. Select Device > Back in the ADL menu (or type Ctrl/Cmd-B) to go back to the trends view. (If you don't see the ADL Device menu, switch to another application, then switch back to ADL.) 10. Quit ADL. Note that some of the tweets are not fully displayed. In the preview release, MobileIconItemRenderer does not resize to the height of its content, which is why we added an explicit height above. This will be addressed for the final release.
<s:MobileIconItemRenderer iconField="profile_image_url" iconWidth="80" iconHeight="80" labelField="" messageFunction="getTweetText" height="110" verticalAlign="top"> <fx:Script> <![CDATA[ private function getTweetText(item: Object): String { return item.from_user + ": " + item.text; } ]]> </fx:Script> </s:MobileIconItemRenderer>
If you run the application again, you'll see the name of each user before each tweet.
Build Your First Mobile Flex Application | 23 2. In the list on the left, select your existing TwitterTrends Desktop configuration and click the Duplicate button at the top. 3. Change the Name to TwitterTrends Device. 4. Change the Launch method to On device. 5. Click Run. The first time you launch, Flash Builder will install the AIR runtime on the device, then install your application. To follow the progress of the launch, you can click on the progress icon in the very bottom right of the Flash Builder workspace, to the right of the progress bar. When the application launches on your device, you should be able to scroll the list of trends and tap on a trend to see its tweets, just as on the desktop, and use the back button on the device to navigate. You should also be able to turn the device to landscape orientation and see that everything works properly, and (if using live data) click the Refresh button on the Tweets view to get the latest tweets.
4. In the Properties panel, set the Width property to 15. 5. Drag an Image into the titleContent to the right of the Spacer and set its Width and Height to 60. (Note: due to an issue in the preview release, the titleContent container box may not look correct when you do this.) 6. Drag another Spacer to the right of the Image and set its Width to 15. 7. Drag a Label to the right of the Spacer. 8. Click the Bold button in the Properties panel.
Your Action Bar should now look like this in Design view:
2. Double-click each label to set the text as in the screenshot above. The labels on the right have placeholder content for now; you'll data-bind them later. 3. Select the labels on the left and click the Bold button in the Properties panel. 4. Select the "(location)" label on the right, and: a. Resize the right edge of the label to near the right edge of the view, leaving some padding.
b. In the Constraints section of the Properties panel, pin it to the left and right edges, as in the screenshot on the next page. (The number on the left may not be exactly the same.)
c. Switch to the Alphabetical view of the Properties panel and set maxDisplayedLines to 1. d. Switch back to the Standard view of the Properties panel. 5. Repeat step 4 for the "(url)" label on the right. 6. Drag in a List below the labels. Move and resize it so that it's flush against the left, right, and bottom sides of the view, and constrain it to all four sides.
Static
data
1. Choose
Data
>
Connect
to
XML
2. For
the
Path
field,
click
Browse
and
browse
to
the
userinfo.xml
file
in
the
src/sampledata
folder.
3. Set
the
service
name
to
UserInfoService.
Leave
all
other
fields
as
the
default
and
click
Finish.
4. In
the
Data/Services
panel
at
the
bottom,
bring
up
the
context
menu
for
getData()
under
UserInfoService
and
choose
Generate
Service
Call.
This
switches
you
to
Source
view
and
shows
that
a
new
getData()
function
was
generated.
5. Rename
the
getData()
function
to
getUserInfo().
6. Right-click/Ctrl-click
on
getDataResult
and
choose
Refactor
>
Rename
7. Change
the
name
to
getUserInfoResult
and
click
OK.
This
modifies
the
name
in
both
places
where
it's
used.
8. Follow
steps
1-7
for
searchUserTweets.xml,
naming
the
service
UserTweetsService.
Rename
the
new
getData()
function
to
getTweets()
and
use
Refactor
>
Rename
to
change
getDataResult
to
getTweetsResult.
Live
data
1. In
the
Data/Services
panel,
bring
up
the
context
menu
for
TwitterService
and
choose
Properties.
2. Click
the
Add
button
above
the
Operations
table.
3. Change
the
existing
name
"Operation1"
to
getUserInfo
4. Change
the
URL
of
the
operation
to
http://api.twitter.com/1/users/show.xml
5. Click
the
Add
button
above
the
Parameters
table.
6. Change
the
parameter
name
to
screen_name.
Make
sure
to
press
the
Return
key
after
the
parameter
name.
7. Click
Finish.
8. In
the
Data/Services
panel,
bring
up
the
context
menu
for
getUserInfo()
and
choose
Configure
Return
Type
9. Make
sure
Auto-detect
is
selected
and
click
Next.
10. In
the
Enter
Value
column,
set
the
screen_name
parameter
to
Adobe.
11. Click
Next.
12. The
type
should
be
prefilled
as
User
by
default.
Click
Finish.
13. From
the
context
menu
for
the
getUserInfo()
method,
choose
Generate
Service
Call.
This
switches
you
to
Source
view
and
shows
that
a
new
getUserInfo()
function
was
generated.
14. Generate
another
service
call
for
the
existing
getTweets()
method.
You'll
use
this
service
call
to
search
for
the
tweets
from
the
given
user.
Call
the
service
when
the
user
navigates
to
or
refreshes
the
view
As
in
the
trends
view,
you'll
do
this
by
implementing
the
refresh()
method.
3. Inside
the
<fx:Script>
block,
add
the
following
method:
Static
data
public function refresh(): void { getUserInfo(); getTweets(); }
Live
data
public function refresh(): void { getUserInfo(String(data)); getTweets("from:" + String(data), "recent"); }
In the next few steps, you'll manually type the code to bind the rest of the UI elements to data. 1. Locate the Image in the <s:titleContent> tag and set the source to {getUserInfoResult.lastResult.profile_image_url} 2. Set the text of the Label in the <titleContent> to {data}. Later we'll pass the screen name of the selected user into this view as its data. 3. For the (location) placeholder label, change the text property to {getUserInfoResult.lastResult.location} 4. For the (website) placeholder label, change the text property to {getUserInfoResult.lastResult.url}
Build Your First Mobile Flex Application | 33 The session caching maintains the entire history of the view stack as well as the appropriate data. From the user's point of view, it looks like the application was never interrupted. You can test this on device as well, but you'll need to manually kill the application in order to test it. On Android, you can do this from the Settings > Applications > Manage Applications screen.
This time, the application restarts from the initial trends view. To go back to preserving session state again, go back to the launch configuration dialog and uncheck Clear application data on each launch. This checkbox also works in device-based launch configurations.
For more tutorials and examples, visit the Flex and Flash Builder Mobile Development page on Adobe Labs.
These device driver configurations are listed in android_winusb.inf. Windows Device Manager accesses this file when installing the device driver. Flash Builder installs android_winusb.inf at the following location: <Adobe Flash Builder Burrito Home>\utilities\drivers\android\android_winusb.inf You can update android_winusb.inf wth USB drivers for Android devices not listed above; see the next section. To install the USB device driver for an Android device, follow these steps. (Note that these steps may be slightly different on different versions of Windows.) 1. Ensure that USB debugging is turned on on your device, and that your device is in "Charge Only" mode with mass storage turned off, as described in Exercise 8 above. 2. Connect your Android device to your computers USB port. 3. If a message bubble pops up indicating that the device driver can't be found, close it. 4. Go to Control Panel and bring up Device Manager. 5. Locate the entry for your device. It should have the name of your device's manufacturer and a reference to "ADB". It will also likely have a warning icon on it, indicating that the driver is not properly installed. 6. Right-click on the item and choose "Update Driver Software". 7. Choose to manually browse to the driver. 8. Browse to this folder in the Flash Builder installation folder: <Flash Builder>/utilities/drivers/android/ 9. Follow the steps in the wizard to finish installation.