Flex Blog

Search:
  • Home
  • Examples
    • Thumb

      Flex Examples

      Check out our Flex Examples!

    • Thumb

      Flash Builder Examples

      Check out our Flash Builder Examples!

    • Thumb

      AIR Examples

      Check out our AIR Examples!

    • Thumb

      Flex Mobile Examples

      Check out our Flex Mobile Examples!

    Adobe® Flex, Adobe® Flash Builder and Adobe® AIR are registered trademarks of Adobe Systems.
  • Components
    • Thumb

      WP Flex Contact Form

      Check out our WP Flex Contact Form!

    • Thumb

      Flash CountDown Plugin

      Check out our Flash CountDown Plugin!

    This is an overview of all our Flash/Flex based Components.
  • Jobs
  • Flex Books
  • Forum
  • Contact Us
Subscribe to Flex BlogSubscribe
  • Examples
  • iOS
Browse > Home / Examples / Using Swipe Gestures in Mobile Flex Application (AIR for Android)

Using Swipe Gestures in Mobile Flex Application (AIR for Android)

22 November 2010

Line Break

Author: Arjan (47 Articles) - Author Website

Arjan is a SAP Consultant specialized in ABAP and Front End development techniques like Web Dynpro, Adobe Interactive Forms, Flex and AIR. In his free time he likes to create examples for Flex-Blog and other applications using Flex, AIR and PHP. Other hobbies are movies and music. He is also the co-owner of Flex-Blog.com.


In my previous posts about Flex Mobile / AIR for Android I talked about navigation and transitions between views in a mobile application. This posts focuses on one of the great benefits of the things that devices are capable of today: gestures. There are a lot of them supported by Flex 4.5 / AIR for Android already. I’m going to give you a simple example for the Swipe Gesture.

When you’re developing a mobile Flex application, you get access to some gesture events that are triggered by AIR running on the device. One of them is the Swipe gesture. Lets create a simple mobile application in which you can navigate between views using the swipe command. Notice that you are going to need an Android device running on 2.2 to test this (our you might have a fancy monitor thats support swipe gestures ;) ).

Create a new Flex Mobile project first, I called it SwipeGestureExample.

Now create a new view that’s called NextView or something, doesn’t really matter. Create a label inside of it that says “Next View” or something like that so you can identify the view when you are running the application.

My NextView.mxml

1
2
3
4
5
6
7
8
9
10
11
12
<?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="Next View">

    <s:layout>
        <s:VerticalLayout verticalAlign="middle" horizontalAlign="center"/>
    </s:layout>
   
    <s:Label text="Next View"/>
   
</s:View>

Also create a Previous View that looks the same as the Next View. Mine looks like this:

1
2
3
4
5
6
7
8
9
10
<?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="Previous View">
    <s:layout>
        <s:VerticalLayout verticalAlign="middle" horizontalAlign="center"/>
    </s:layout>
   
    <s:Label text="Previous View"/>
</s:View>

With that View created, lets go back to the default home view created by Flash Builder Burrito and add a label to that as well.

My Home View:

1
2
3
4
5
6
7
8
9
10
11
12
<?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="Home View">

    <s:layout>
        <s:VerticalLayout verticalAlign="middle" horizontalAlign="center"/>
    </s:layout>
   
    <s:Label text="Home View"/>
   
</s:View>

Now for the fun part, create a script block on the Home view and create a method called init that’s executed when the initialize event is triggered. This init method will put an event listener on the Swipe event. Every time a swipe is done on the device, that method is automatically called. Also create the skeleton for the event handler.

The init method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<fx:Script>
    <![CDATA[
        private function init():void
        {
            this.addEventListener( TransformGestureEvent.GESTURE_SWIPE, handleSwipe ); 
        }
       
        private function handleSwipe(event:TransformGestureEvent):void
        {

        }
       
    ]]>
</fx:Script>

Notice that the event we are listening for is of type TransformGestureEvent.GESTURE_SWIPE. Let the event handler receive that type too, we need a property of it that is unique for this type.

The event has a property called offsetX. This is the property that we are interested in to determine whether the swipe was to the left or to the right. When this property is -1, the swipe was to the left, when its 1, to swipe was to the right. Lets do something with it. Add this code to your TransformGestureEvent.GESTURE_SWIPE event handler called handleSwipe:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private function handleSwipe(event:TransformGestureEvent):void
{
    // Swipe was to the right
    if (event.offsetX == 1 ) {
        // push the PreviousView without any data using default
        // ViewTransition
        navigator.pushView( PreviousView, null,
            new SlideViewTransition( 300, SlideViewTransition.SLIDE_RIGHT ) );
    }
    // Swipe was to the left
    else if (event.offsetX == -1 ) {
        // push the NextView withour any data using
        navigator.pushView( NextView, null,
            new SlideViewTransition( 300, SlideViewTransition.SLIDE_LEFT ) );
    }
}

To go back to the home view from either the NextView or the PreviousView, you can also implement this logic on those views a well. You can also just use the back key on your Android device or use a button, doesn’t really matter…you get the idea :) .

So that’s it. Pretty simple to use swipe in your Mobile Flex application right? Stay tuned for more examples.

EDIT:

Instead of using the initialize method to add an event listener for the Swipe gesture. You can just put an event listener on the gestureSwipe event on the View tag like this:

1
2
3
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        title="Home View" initialize="init()" gestureSwipe="handleSwipe(event)">

Related posts:

  1. TabbedMobileApplication Example in Flex Mobile (AIR for Android)
  2. View Navigation in a Mobile Flex Application
  3. Flex Mobile: Two finger tap gesture to toggle actionBar visibility in a View (AIR for Android)
  4. CheckBox in List using MobileIconItemRenderer for Flex Mobile
  5. Flash 10.1 / AIR Mobile: What device do you need?

Written by Arjan · Filed Under Examples 

Was this post useful to you?

Please rate this post, follow us @ twitter, or link to this page from your website!

1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 4.80 out of 5)
Loading ... Loading ...

1adcbe912d363a0a9f8e47aa19e33219delicious

Comments

7 Responses to “Using Swipe Gestures in Mobile Flex Application (AIR for Android)”

  1. greg on November 23rd, 2010 1:45 am

    Very cool. I’m a laggard getting my Android phone, but can’t wait to play with the new gestures interface. Is pushView similar to a viewStack, and can that be coded in mxml too?

    In a slight bend, I just rewrote a kinetic scroller for, I guess, non-mobile apps that adds very simple gesture effects to regular Flex apps. Check it out at http://www.softwareodyssey.com/blog.aspx

  2. Arjan on November 23rd, 2010 1:59 pm

    Hi greg,

    The view mechanism in Flex Mobile is a bit similar to the viewstack in the sense that it is a stack of views, but they’re completely different. So you cant code a stack of views for mobile in MXML like you do with a viewstack…

    About your kinetic scroller: cool component. I might use it some day.

  3. dagi3d on January 4th, 2011 9:03 pm

    It worked fine on my android phone but I’m unable to capture the event while developing on my computer. Is it a builder’s bug? (I’m using mac os)

  4. John on February 10th, 2011 7:43 pm

    I need to differentiate between a tap on a list control and a left gesture swipe. The list control is selected first. Is there anyway that this could be achieved?

  5. Maile on March 23rd, 2011 3:04 pm

    Hi Arjan,

    I maintain Adobe’s Tour de Flex and mobile samples would be a great thing to add to promote 4.5 and be a resource for Flex developers. If you are interested, could you please email me?

    Thanks!
    Maile

  6. Daniel Demmel on April 27th, 2011 1:44 pm

    For anyone who is having problems getting this working, a lot has changed in the final 4.5 SDK, I’ve written a blog post to clarify: http://daaain.posterous.com/transitioning-between-views-in-a-mobile-flex

  7. Andrey on October 21st, 2011 2:08 am

    Thanks for this post, definitely very helpful. Quick “silly” question, how does one test a swipe gesture on Windows with a mouse? It seems to only register click, or drag, but I can’t figure out how to simulate a swipe gesture.

Get Adobe Flash player

  • +1?

  • Support Flex Blog!

  • $ 13 raised
    • 2012/01/13 8:22 PM Russell Brown donated $ 3.00
    • 2011/10/31 4:43 PM Steve Dakin donated $ 5.00
    • 2011/05/11 3:37 PM Roelof Albers donated $ 5.00
  • Stay in touch!

  • Popular Tags

    • AdvancedDataGrid
    • AIR
    • ArrayCollection
    • baseColor
    • Button
    • CursorManager
    • DataGrid
    • Dynamic
    • Effects
    • File
    • FileStream
    • Flash Builder
    • Flash Builder 4
    • Flex 4
    • Flex Mobile
    • Framework
    • Icon
    • Image
    • itemRenderer
    • LinkBar
    • Mobile
    • PHP
    • ProgressBar
    • Repeater
    • Style
    • SWIZ
    • Timer
    • Tree
    • Twitter
    • ViewStack
  • Advertisements

  • Recent Posts

    • Spooky Frenzy – iPad Game
    • Fountain Example
    • Reading & Writing files in Adobe AIR
    • CheckBox in List using MobileIconItemRenderer for Flex Mobile
    • Data Dependent decoratorClass in MobileIconItemRenderer Example
    • Flex 4 Resize Effect Example
    • Jump to next field using the Focus Manager
    • Searching Data using a Class Example
    • Flex Mobile: Two finger tap gesture to toggle actionBar visibility in a View (AIR for Android)
    • TabbedMobileApplication Example in Flex Mobile (AIR for Android)
  • Categories

    • Examples
    • Guest Poster
    • iOS
  • Archives

    • September 2011
    • July 2011
    • May 2011
    • March 2011
    • February 2011
    • November 2010
    • October 2010
    • September 2010
    • August 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • March 2009
    • February 2009
  • Blogroll

    • Adobe Flex Jobs
    • NL for Business
  • Meta

    • Register
    • Log in
    • WordPress
    • XHTML

Copyright © 2010 Flex Blog · Adobe® and Adobe® Flex are registered trademarks of Adobe Systems.

WordPress Adobe Flex Adobe Flash Builder Adobe AIR Creative Commons License

  • Popular Posts

    • Progressbar in Datagrid Example 13 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Data Dependant Tree Icon with Tree in AdvancedDataGrid with iconFunction 8 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 5 (5.00 out of 5)
    • List Directory with AIR in Flex 4 7 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Flex FlashVars in AS3 Example 7 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Flex Dynamic Chart Example 4 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 5 (5.00 out of 5)