New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Start learning with our library of video tutorials taught by experts. Get started

Flex 4.6 and AIR 3.0 New Features for Mobile Apps
Illustration by Don Barnett

Using the ToggleSwitch component


From:

Flex 4.6 and AIR 3.0 New Features for Mobile Apps

with James Talbot

Video: Using the ToggleSwitch component

The new spark ToggleSwitch component defines a typical tablet control for defining a binary switch such as an on/off switch for an application. Clicking anywhere inside of the control toggles the state. We can also slide our thumb along the track to change the state, and we can even set the duration for how long that lasts. The default values for the unselected and selected labels are off if they're unselected or on if they are selected. We can also easily change the labels shown by defining a custom skin.

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
Flex 4.6 and AIR 3.0 New Features for Mobile Apps
1h 49m Intermediate Jan 30, 2012

Viewers: in countries Watching now:

Join James Talbot, as he reviews the new features in Adobe Flex, Flash Builder, and AIR for mobile application developers. This course covers the Spark components for building mobile user interfaces in the Flex SDK, and the AIR support for native extensions, which enable applications to use parts of the host operating systems that were previously unavailable. The course also describes new testing and network monitoring capabilities available with Flash Builder 4.6, and the ability for Android applications to be deployed without relying on a separate runtime.

Topics include:
  • Controlling adaptable tablet layouts
  • Controlling view hierarchy
  • Using 16-bit and 32-bit render modes
  • Using the Date Picker, Scroller, and List components
  • Adding a splash screen to a mobile app
  • Using native extensions for iOS and Android
  • Testing with FlexUnit on Wi-Fi
  • Packaging the application for release
Subjects:
Developer Mobile Apps
Software:
AIR Flash Builder Flex
Author:
James Talbot

Using the ToggleSwitch component

The new spark ToggleSwitch component defines a typical tablet control for defining a binary switch such as an on/off switch for an application. Clicking anywhere inside of the control toggles the state. We can also slide our thumb along the track to change the state, and we can even set the duration for how long that lasts. The default values for the unselected and selected labels are off if they're unselected or on if they are selected. We can also easily change the labels shown by defining a custom skin.

Let's explore how to use the brand-new ToggleSwitch component to build truly compelling tablet-based applications. So the first thing I want you to do is just import the Flash Builder project. This is our 1-5.fxp file. So go ahead and just import that and then click Finish, and I'm just going to run this really quickly to show you what we have. Okay, here is our toggle switch component. We don't have any toggle switches added in in the starting files here, so for those of you who don't have the student files, you can just create a brand-new mobile project and add in the toggle switch components like we'll do in just a second, wherever you like, and you'll get the same gist.

So here I'm going to say mobile application. I'm going to build a mobile configuration for Apple iOS. We'll test it on our desktop, and we'll choose an iPad, and I'll run this. And what we're going to do is we're going to add some ToggleSwitch components, so we can switch whether or not our application is running on port 80 and whether or not it's using WiFi. It'll give the users the ability to slide on and off these two functionalities. So, very cool. And first thing I want you to do is just scroll down to where it says WiFi and add in a new ToggleSwitch component here.

So I'm just going to say s:ToggleSwitch, just like so, and I'm going to just give it an id of let's call it switch1. And then I'll specify my slideDuration. We'll try 100 milliseconds. You can play with it and see what works best for your application of course. And then I'll add a change event, and I'll just simply say change is equal to-- and let's create a new function here called toggleswitch_1, and we'll just call it change.

And then here, I'll make sure that of course I'm passing that the event object, just like so, and then I will close off my ToggleSwitch component. So now let's add in a script block right at the top of our application, and I'll just say fx:Script, just like so. I'll say protected function. I'll make sure that matches the same function, so I'll just say toggleswitch_1change, just like so. And I'll say event:Event, and then I'll specify void, because this method won't actually be returning anything, and there is my new ToggleSwitch change event.

Now let's also add a ToggleSwitch MXML directly under our port 80 here as well, and we'll just make this easy. We'll copy this same thing here, and I'll copy that over here, and I'll paste that directly under port 80 here, like so. And instead of calling it switch1, I'll call switch2, and I'll call the same event handler. So now go up here to my ToggleSwitch event handler and you'll see down below there is a label control with the id of LBL.

So let's just populate this lbl and I'll say .text will populate the text property that label control. And I'll say is equal to the event object. We'll just say target.id, so this will of course be whichever toggle switch component, whether it was 1 or 2, that I happen to click on. And I'll say plus, you can do a concatenation there, and I'll say selected is equal to, close that, and then I'll say plus event.target.selected, just like so. And now that should populate that with the toggle switch that I actually clicked on.

So let's explore that. I'll save this. You should see that it compiles without any problems. We'll go back to my run configuration. Make sure you have a Run Configuration for an iPad, click on Run, and you'll see here are my new toggle switch components. Very cool. Click here and you can see that it's either selected true or false, and again, you could see that also the port 80 is selected true or false. Now, we can actually skin our toggle switch component so we can make these look even better, and we can have a lot of creative control over how these switch components look.

So let's explore how to do that. And what I want you to do is just create a new package here called skins. So I'm just going to say New > Package, and we'll call this skins, and I'll click Finish. And then what I'm going to do is I'm going to build a new file called MyToggleSwitchSkin. So I'm going to simply say New, and we'll just say File here to make it easy. We'll see new file, and we'll simply call this MyToggleSwitchSkin.as, for ActionScript of course, and we will simply build a new ActionScript class for skinny.

So I'm just going to simply say package skins, like so, and then I'm going to say import spark.skins.mobile.ToggleSwitchSkin. So this is importing the existing skin that we're already using, so we don't have to re-create the wheel there. So we're going to import that, and then we're going to define our skin class. So I'll say public class MyToggleSwitchSkin extends ToggleSwitchSkin, just like so, define my class there, and then of course, I'll define my constructor.

So I'll say public function MyToggleSwitchSkin, like so, and then I'll call the constructor of the toggle switch skin class, so I'll just say super to call that. And then I'll say selectedLabel, and I'll specify is equal to Open. Again, that's all defined in the original class. And then I'll just say unselectedLabel is equal to Closed. Then I'll put in that string literal there, Don't forget your semicolons. And just an example of how to do a very simple skin.

So go ahead and save that, go back to your ToggleSwitch.mxml, and now go to your second toggle switch and add a property here called skins class. So I'm going to say skinClass is equal to, and I should be able to just say skins, and you'll see the code introspection automatically happens. So I'm going to say MyToggleSwitchSkin, like so, and then I'll say the slide duration.

I'll put all of that in, and then I'll go ahead and hit Save. And now let's again run this on our simulated iPad, and then I'll click Run. And notice you should now see that I've modified that skin, instead of saying on or off, I'm saying Open and Closed by creating that custom ActionScript skin. So, very, very cool, and again, yet another component that really allows us to build great tablet-based applications.

There are currently no FAQs about Flex 4.6 and AIR 3.0 New Features for Mobile Apps.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Flex 4.6 and AIR 3.0 New Features for Mobile Apps.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Notes cannot be added for locked videos.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.