Skinning a Flex Mobile Button with Bitmaps

Flex Mobile Button Skin

Flex Mobile Button Skin

I’m working on a mobile app that requires a lot of skinning and since Flex 4 we’ve gotten a MUCH improved skinning model for Flex. Unfortunately, with Flex mobile, for performance reasons it’s a good idea to use ActionScript-only skins which means going back to the world of Flex 3 and diving into the component lifecycle a bit.

My colleague Terry Ryan has a good blog post that shows what he used to do skinning and I started off with that approach. But something didn’t seem quite right about skinning a button with a function associated with borders, so I tracked down the skinning master on the Flex team, Jason San Jose, and asked him the best way to do it. It turns out it’s really pretty simple to skin a button using Bitmap assets. If you look at the source code for ButtonSkin you’ll see that in the constructor, they associate FXG files (as classes) with specific button states.

upBorderSkin = spark.skins.mobile320.assets.Button_up;
downBorderSkin = spark.skins.mobile320.assets.Button_down;

In that constructor they also check for the DPI of the device and then load in the correct skin class according to that DPI. In general it’s going to be a good idea to check for different screen sizes and load in different assets accordingly, but for this example, I’ve just got one size that stretches reasonably well. The first step is to embed the assets and give them a variable name.

[Bindable]
[Embed(source="/assets/combobox_button_up.png")]
private var up:Class;
 
[Bindable]
[Embed(source="/assets/combobox_button_down.png")]
private var down:Class;

Then, in the constructor, simply set the upBorderSkin and downBorderSkin to those classes:

public function ComboBoxButtonSkin()
{
     super();
 
     upBorderSkin = up;
     downBorderSkin = down;
}

In this example I also wanted to create a graphic that would be overlaid on top of my skin. It’s a triangle graphic that will change depending on whether the app is on iOS or Android because of the different ways that those two platforms do ComboBoxes. On iOS you often have the app slide over to reveal the list (so the arrow would be pointing to the right) and on Android you often get a pop-up window with a list of choices (so the arrow would point down).

Adding Objects To Your Button Skin

To add something to your skin you need to go back and think about how Flex components work. You first want to make sure it’s added using the createChildren method. In this example I have already created the _triangle variable and instantiated it in the constructor.

override protected function createChildren():void
{
     super.createChildren();
     addChild(_triangle);
 
}

Then, once it’s added, you can start to modify it. In this example I wanted to make sure I could dynamically place the arrow depending on the height and width of my button. Luckily, the drawBackground method gives you an unscaledHeight and unscaledWidth, which you can use to calculate where the object should be placed. So all I had to do was tie into the drawing API, use those numbers, and voila, my skin now has the triangle.

override protected function drawBackground(unscaledWidth:Number, unscaledHeight:Number):void
{
     var halfUnscaledHeight:int = Math.round(unscaledHeight/2);
 
     _triangle.graphics.beginFill(0xcee410);
     _triangle.graphics.moveTo(unscaledWidth-20,halfUnscaledHeight-5);
     _triangle.graphics.lineTo(unscaledWidth-10,halfUnscaledHeight);
     _triangle.graphics.lineTo(unscaledWidth-20,halfUnscaledHeight+5);
     _triangle.graphics.lineTo(unscaledWidth-20,halfUnscaledHeight-5);
     _triangle.graphics.endFill();
}

One of the benefits of this approach as opposed to just including the triangle in the skin is that the triangle kind of looked odd when it was stretched and morphed to different screen sizes. This way it’s independent of the bitmap skin and I can change the size according to the size of the underlying button.

Here’s what the finished button looks like and here’s the full code for you to grab.

Mobile Button Up State

Mobile Button Up State

Mobile Button Down Skin

Mobile Button Down Skin

    package skins
    {
    import flash.display.Sprite;
 
    import spark.skins.mobile.ButtonSkin;
 
    public class ComboBoxButtonSkin extends ButtonSkin
    {
    [Bindable]
    [Embed(source="/assets/combobox_button_up.png")]
    private var up:Class;
 
    [Bindable]
    [Embed(source="/assets/combobox_button_down.png")]
    private var down:Class;
 
    protected var _triangle:Sprite;
 
    public function ComboBoxButtonSkin()
    {
    super();
 
    upBorderSkin = up;
    downBorderSkin = down;
 
    _triangle = new Sprite();
    }
 
    override protected function createChildren():void
    {
    super.createChildren();
    addChild(_triangle);
 
    }
 
    override protected function drawBackground(unscaledWidth:Number, unscaledHeight:Number):void
    {
    var halfUnscaledHeight:int = Math.round(unscaledHeight/2);
 
    _triangle.graphics.beginFill(0xcee410);
    _triangle.graphics.moveTo(unscaledWidth-20,halfUnscaledHeight-5);
    _triangle.graphics.lineTo(unscaledWidth-10,halfUnscaledHeight);
    _triangle.graphics.lineTo(unscaledWidth-20,halfUnscaledHeight+5);
    _triangle.graphics.lineTo(unscaledWidth-20,halfUnscaledHeight-5);
    _triangle.graphics.endFill();
    }
 
    }
    }

Enabling iOS/BlackBerry/Android After Creating a Flex Mobile Project

I just noticed this today and figured it might be helpful. If you create a Flex Mobile project with Flash Builder 4.5.1 and choose to only enable one or two of the platforms, you can always go back and change your mind later on using the Properties Panel under Flex Build Packaging. Just select the platform you want and check the “Enable this target platform” box.

SourceMate 3.0 Released

SourceMate was by far my favorite plugin for Flash Builder 4 and they’ve been just as busy with Flash Builder 4.5. They just released SourceMate 3 today and it looks like it’s going to be very helpful for Flash Builder 4.5 users. Definitely check out the full feature list but some of the ones that look most interesting to me are:

And the localization features look really helpful for anyone building applications that need to make use of resource bundles for localized apps.

Major props to the SourceMate team.

Join us for MAX 2011! (And the Loyalist Discount)

The site for MAX 2011 is up and has a ton of info about what’s in store for October. It’s going to be tough to match next year but with the launch of CS 5.5, the new mobile workflows in Flash Builder and Flex, plus a big focus on HTML5 and Gaming, there really is going to be something for everyone.

And if you were with us last year, we’ve got a special deal for you; our loyalist discount.

You have to use these before May 31st in order for them to work, but if you miss that you can still register at the early bird price before July 31st.

MAX has always been one of my favorite conferences of the year but last year it was really special. Between Kevin’s keynote, the device giveaways, and the excitement of the attendees, it was an energizing experience. So I hope you can all attend this year.

Beer List for Flash Camp 2011

Most of you know that I’m a pretty serious beer person. And since we have a fantastic beer store, City Beer, just up the street from the SF office, it makes it easy to get good beer for events like Flash Camp. I just finalized the list and we’ve got some great beers. At this point you should have no excuse; it doesn’t get better than mobile development and great beer.

We’ve got some great sessions, the product teams are coming to help answer questions, and it will be a lot of fun to show off the Flex mobile story in person.

Flash Camp San Francisco – April 30th

I am really excited to announce that this weekend we’re going to have a free, all day FlashCamp in San Francisco to celebrate the launch of Flex 4.5 and Flash Builder 4.5 starting at 9:00 AM, on April 30th. This will be a little bit different from some of our past Flash Camps that we have held, because it is during the day. However, this allows us to do some more hands on sessions as well as holding a coding session and contest for Flex 4.5 mobile application development

In the morning we’re going to have sessions covering all aspects of the mobile workflow. We’ve got a keynote from VP of Developer Tools, Ed Rowe, then Deepa Subramaniam, Flex Product Manager and Andrew Shorten, Group Product manager for Flex, are going to be doing an overview of the new mobile-specific features in Flex 4.5. We’ll also have 3 separate tracks to help you dive a little deeper:

  • Flash Builder for PHP
  • iOS and PlayBook development
  • UX design for mobile devices

In the afternoon we’ll bring out the tables and let you guys get to work building mobile apps. The Flex and Flash Builder teams will be on hand to help answer questions and help provide feedback.

The beer is being provided by City Beer Store, and I’m still nailing down the list, but we should have some fantastic craft beer at the event as well as pizza provided by Amici’s to help fuel the end of the day coding before we start giving away prizes and showing off mobile applications.

The Contest

One of the unique things about this Flash Camp is that after the hands-on session we’ll be giving away some prizes for people who have built a mobile application that afternoon. We’ll have a bunch of categories including most interesting use of Flex, most useful application, and most-well designed application. And you can win software, devices, an Xbox Kinect, and more, so there’s a ton of potential loot for you to take away.

You can also feel free to form teams, so if you are a designer-type, bring along your developer friends to fill out the team and you can show off the application as a team at the end of the day.

It’s going to be a lot of fun and I can’t wait to see what you guys put together. So register and come learn about mobile development with Flash and Flex, drink some great beer, and win prizes.

A Couple of PlayBook-Related Videos on AdobeTV

The PlayBook is on sale today and it really is a beautiful device, especially for Flash developers. Having been able to play with one for a while now I am incredibly impressed with the QNX operating system and the general form factor of the tablet.

I did a couple of videos on the PlayBook for AdobeTV. The first one is on using OAuth with the PlayBook. It should be a good primer for PlayBook developers who want to leverage services like Twitter or Facebook Connect. The second one was a video of one of Renaun’s tips on how to use ImageCache in a QNX list to help improve performance. Both are embedded below.

Slides and Code from 360Flex Denver

360Flex Denver360|Flex Denver was an absolutely fantastic event. I was at the first 360 and have watched the event grow and mature over the years. It’s always been the best Flex conference out there, but there was something about those first few 360′s that had so much energy and got the community excited. This event felt like that. I’d rank it as one of the best 360′s I’ve ever been to. The attendees were enthusiastic and great to talk to, the keynotes were awesome and inspiring, and John and Nicole were at the top of their game as far as hosting went. It might have been all of the mobile stuff, it might have been the announcement on day 2 of Project Spoon, but this 360Flex was just all around great.

So of course, I made a bit of a misstep with my talk. At what was one of the greatest Flex conferences of the past 5 years, I go and give an HTML5/jQuery talk. But hopefully it was still fun. And to be honest, as I dove into HTML5 and jQuery for this talk, I learned that we have it very, very good as Flex developers. There are a lot of great things about HTML5 and especially jQuery, but if you’re building complex RIAs and applications, Flex has solved a ton of problems that the HTML5/JS world hasn’t yet. I definitely think they will, and the community around JS and jQuery is great, but as a Flex developer, you’ve got the best of both worlds and you’re going to be able to jump over to jQuery/HTML5 projects and bring some very valuable knowledge about building complex apps.

Thanks to everyone who attended the talk. My slides can be found here (and embedded below) and you can grab the demos from my GitHub repository here.

The Implications of Every Flash Developer Being a Mobile Developer

There’s an article in the Wall Street journal today about the demand and insufficient supply of mobile developers, which is becoming a huge problem for companies as mobile strategy becomes more and more critical. One of the main problems, as the WSJ draws out, is that these mobile platforms are relatively new so it’s tough to find developers with a lot of experience. Many companies are turning to good developers and retraining them as mobile developers to fill demand.

If a software engineer doesn’t have mobile experience, the company has sometimes been willing to spend several weeks training the engineer to work on mobile platforms, Mr. Rosenthal said.

Given the mismatch between supply and demand, many companies say they have no choice but to retrain software engineers in the art of mobile development. In the last year, Major League Baseball’s Internet company MLB.com nearly doubled the number of mobile engineers it has to 19, said MLB.com CEO Bob Bowman.

I thought this article was a perfect complement to the announcement this week of Flash Builder 4.5 and Flex 4.5, which are focused explicitly on helping Flex developers build applications for mobile devices like iOS, Android, and the PlayBook. There is obviously huge demand for mobile applications, and because of scarce supply and experience, existing developers are going to have to think about how they can gain mobile experience. Flex mobile does a fantastic job of making that learning curve smaller by letting Flex developers use what they already know and providing some key mobile features (like ViewNavigator for managing views, the ActionBar for managing global navigation, etc) so that they can quickly turn out Flex applications for these mobile devices.

If you’re a Flash or Flex developer, then with AIR for mobile devices and Flex 4.5, you’ve got the skills to go out and build applications for the biggest platforms. That’s a huge advantage to you as a developer and for any developer who doesn’t want to get locked into a specific platform. As the chart below shows, there are a lot of people looking for experts in iPhone, Android, and BlackBerry, but there are a lot more people who are looking for Flash experts. With this release, you get the best of both worlds.

Slides and Assets from Adobe Refresh

Adobe Refresh 2011

Thanks to everyone who came out to Adobe Refresh. It was a fantastic time for me and I got to talk to a bunch of you who had some very good insights into what you want to see from us going forward. I also got to see some great demos, so thanks for showing off your stuff. For those who asked, I’ve posted the slides and the samples for my talk. You can grab the slides from SlideShare: Flash Builder and Flex Future – Building Multiscreen Applications. I’ve also posted the two demos I used.

  • GeonamesBrowser – A demo of the Flex “Hero” components/paradigms that uses the GPS on the phone to grab a list of area landmarks from Geonames.
  • CollaborationMobile – A demo showing off LiveCycle Collaboration Services on a mobile device.