Heartland Developers Conference – Next Week

Next Week I’ll be at the Heartland Developers conference with a bunch of other great presenters and colleagues.

Develop an Android Application With Me

On Wednesday I’ll be teaching a 4 hour lab on How to develop an Android Application. Sign up soon, as there are only a few tickets left for this class! The best part? Its only 25 BUCKS! Can’t beat 4 hours of instruction from a seasoned Android pro for so cheap!

I will be posting the links to the finished modules on my GitHub page (as well as on here on my blog) in the coming days. This will allow you to see the code before we actually start writing it. You will be able to view each module as it is completed via the different branches in the Git repository. You don’t need to know Git, I’ll give you direct download links.

Develop An Android Application Presentation

Have prior engagements or simply can’t make the hands on lab? Then come down on Friday and check out my presentation on how to develop and Android application. I will be creating a new application in class and then posting it live to the Android Market. More info on this session can be found here.

I’ll be in Omaha NE

If you’d like to meet up during the week, before or after the conference to talk tech, enterprenuerial goodness, or just shoot-the-shiz – let me know. I’ll be hanging out downtown with the rest of the presenters and so forth. I’d love to meet you and interact.

HDC10 Hash Tag

Don’t forget, we’ll all be tweeting (presenters, attendees, sponsors, etc) about the event. Use the #HDC10 hash tag when tweeting about the event! It makes it easier to communicate. Feel free to follow me on twitter – @donnfelker

See you next week!

ModelBinding ASP.NET MVC and Multiple Field Validation

This tip comes to you from my blog, but the hat-tip goes to Andres Nelson whom I work with at my current client who actually showed me how to do this.

The Scenario

You have a grid with multiple fields. These fields are dependent upon each other. If one field is empty, then the other field is empty. At that point you want to display ONE error message informing the user that something is wrong, but you want to highlight both fields, as shown below in Figure 1-1. Please excuse the blurring of everything, but its NDA, you know how it goes. :) Click for a larger view.

Figure 1-1: Highlighting both fields, but providing one error message.

Solution

I never thought of this, but its actually pretty simple, so again – hat tip to Andres. Simply add two ModelState errors, one for each property you want highlighted. However, for the second field, provide and empty string. MVC will not show that error in the validation summary, but the field will still get highlighted. Here’s the code -


bindingContext.ModelState.AddModelError("Field1Name", "Hey! Bad Stuffs Happens!");

// The one below will not show up in the validation summary, but the field will be highlighted.

bindingContext.ModelState.AddModelError("Field2Name", String.Empty);

That’s it!

MacBook Pro – Windows 7 Boot to VHD

Anyone who knows me personally knows that I run all of my work in a virtual environment. Back in 2009 when Hanselman posted about the Boot-to-VHD option of Windows 7, I had it up and running by the next morning. I’ve not stopped using it since.

My Setup

I have VHD’s for:

  • Different Clients – Therefore I can join their domain and have a computer (VHD) dedicated to their setup. Since each client usually requires a particular set up that normally conflicts (if I were running a single machine)  with another client, booting to a VHD allows me to have various configurations with different tools and sets up.
  • Beta products – I’m in the process of creating a Windows Phone 7 app, yet the tools are in Beta. We all know about Windows Tools and the “beta” work – AKA:Your stuff might come to a crashing halt. Therefore I have a couple VHDs for testing various environments and tools.
  • Android – I have a VHD that I use specifically for Android development.
  • Personal Use – Sometimes I need to develop code in VS2010 or Eclipse or RubyMine and would like it to be separate from the rest of the code I have. This is where I put this stuff.

This process has worked great and I love booting to VHD, I think its one of the best things about Windows 7, hands down.

Can I run it on a Mac?

I’ve never been a Mac person until recently. My wife got a mac about a year ago and I’ve only used it to surf the web and fix some minor issues she had with printing. Other than that, I haven’t really touched it. However I have the need to develop iPhone and iPad apps as I’ve been getting a ton of inquires at my mobile devleopment firm – Agilevent to do so. My Dell D630 was on the way out (video card was pooping out on me) and it was time to buy a new PC before Heartland Developers Conference, which I’ll be presenting at in less than a month.

So, with some advice from Aaron Kardell (thanks Aaron) I picked up a refurb MacBook pro from the Apple Store online.

Booting to Windows and Creating a VHD

I knew that with the MacBook Pro I could run Windows 7 (as it was Intel based). This was possible to do through Bootcamp. This is something thousands of people do every day. Nothing big. However I wanted to install Windows 7, and then create multiple VHD’s for the MacBook pro environment. I followed the instructions in the Bootcamp documentation to set up Windows 7 on a different partition. I have a 500 GB HD, so I split the space equally between OSX and Windows 7. I then installed Windows 7 on the Windows partition.

When you boot a MacBook Pro with Bootcamp, you’ll get two options (if you hold down the options key on the Mac while booting). One is the Mac partition, the other is the Windows partition. I select the Windows partition to boot to Windows. Once I was able to do that, I followed Hanselman’s instructions to create a 80GB expandable vdisk using diskpart after booting to the Windows 7 DVD. This allowed me to create a virtual disk which I was able to install Windows 7 to.

When you select this new partition in the Window setup, it will warn you that you cannot (or likely should not) install windows because of possible driver issues. Blaaaah… nonsense, do it anyway. :)

After I let the install rip for awhile it was done. The system did reboot a couple of times and here’s where the a small problem started. When I added the vdisk and attached it, Windows gave it the boot loader name of “Windows 7″. However my default instance was also named “Windows 7″. Therefore when I booted into Windows 7 I had TWO “Windows 7″ options. I eventually figured out which one was the correct one – but watch out for this because it can be tricky.

Fixing the Same Name Issue

After everything is installed, you’ll want to go back into your default Windows 7 installation and fire up bcdedit from the command line.

Issue the command:


bcdedit /v

This will show you all of the entries in the boot loader for Windows. On one of those you’ll see a path to your .vhd file. That one is your VHD boot. Grab the id (the long guid deal) and copy it to the clipboard. Then type the following command -


bcdedit /set {GuidGoesHere} description "VHD"

Of course, replace the “GuidGoesHere” with the guid you copied from the previous step, also give it a different name other than VHD if you wish.

After that, issue another bcdedit /v command and you’ll see the name has changed. Now, when you reboot into the Windows partition you’ll see the following two options -

  • Windows7
  • VHD

Creating Multiple VHD’s

Once your VHD is set up, you’ll want to boot back into your default Windows 7 install and then copy the .vhd file somewhere safe. This will be your boilerplate Windows 7 install. At  a later time if you’d like to have ANOTHER VHD to boot from (maybe to test beta software, you go to a new client, etc), you can copy this same VHD into a new folder on the default Windows 7 install, perform the bcdedit commands to add the VHD to your boot loader (as listed in great detail in Hanselmans post) and now you’d have three options to boot from:

  • Windows7
  • VHD
  • <YourNewEntryHere>

Booting to the VHD

To recap, I have a MacBook pro, which I’ve used Bootcamp to install Windows 7. Once that was installed I created a VHD (after choosing Windows 7 from the initial boot loading screen). I have created, perhaps, many VHD’s to boot to, and used the bcdedit tool to add them to my Windows boot loader options.

To boot the any of the VHD’s I perform the following steps -

  1. Power on the Macbook Pro while holding down the option key
  2. Select the Windows Partition to boot into.
  3. Once Windows start to boot I will receive an option of what Windows installation I want to boot into (the default “Windows7″ or any of the VHD’s)
  4. I select the VHD, and Windows starts, running with full hardware support*

Its that simple. I can now copy over a new VHD, install some software and test it out.

* Drivers & Full Hardware Support - Because the MacBook Pro hardware is different than a regular PC, you’ll need to put the OSX install CD into the drive and install the Bootcamp tools for Windows. This will install ALL of the hardware drivers you need. You will then be able to use the “command” key on the MacBook Pro keyboard AS the “Windows Key” as you would on a normal Windows keyboard. I advise that you install the Bootcamp tools and then create a copy of the VHD as your baseline/boilerplate VHD. This will allow you to quickly copy over the VHD and get up and running smoothly.

Visualizing the VHD Install

To help you understand what just happend, I’ve created the diagram in Figure 1-1 below. Click it for a larger view.

MacBook Pro, Windows 7 and VHD

Figure 1-1: Visualizing the VHD install. OSX and Windows are boot options. If booted to Windows 7, the other options then become available during the boot process of Windows

Android Library Project Reference Not Showing

I previously posted about how you could make a free and lite version of your application using the Android Library project feature inside of Eclipse.

I set out to do this a few days ago with a client project that I had been working on. My development work flow went like this:

  1. Build the entire app in one project first. All features.
  2. Turn the said app into a library
  3. Build out the Full and Lite shells
  4. Crank out the Full and Lite versions as APK’s so I can get them on the Android Market.

The Problem

However, wen I got to part 3 I ran into an issue. The Android Project would not reference the Library. However, in the gen/ folder I could see that the R.java file was being generated by Eclipse with the resources from the referenced project. However, there was no referenced project icon and anytime I added anything to the ApplicationManifest.xml file Eclipse would complain that it could not find the class.

It seemed that Eclipse ‘knew’ about the project because it could build the R.java file, but could not fully find it. It was somewhere in Eclipse limbo.

Solution

After about 6 hours of trying various things I figured out what the issue was.

The problem: My projects in Eclipse had spaces in them (note, I’ve found that periods also cause a problem).

I changed the name of my projects to not have any spaces or punctuation in them. Such as “FooLibrary” instead of “Foo Library” or “Foo.Library”. Then I de-referenced the project, then referenced it again. It still would not show up. Sometimes restarting Eclipse can make everything “bind” again. Not sure why, but it seems to be a bug with the ADT plugin. Long story short, after restarting Eclipse, the referenced project was now available and working as it should.

Note, this is only the project name in Eclipse, not the Java package name.

Conclusion

Moral of the story – don’t use spaces, periods or any punctuation in your projects or libraries if you want to use Android Library Projects.

I wish the Android Documentation had a comment system so that I could post this there as it would have saved me 6 hours and a lot of frustration because spaces in the name of a project being the root cause is not too intuitive.

HowTo: Android – Full and Lite Versions

A full working code example of this post is located on my GitHub account here: http://github.com/donnfelker/FullAndLiteVersionSharedLibrary

I’m in the process of completing an Android app for a client and they needed the ability to have full and lite versions of the same application. The lite version would be free and the full version would be for a small fee (say, $2.99).

The Android Market stores applications based upon their unique Java package name. Therefore you cannot have multiple apps with the same package name. Therefore you’re left to do one of two things-

  1. Create two Android projects and copy the code, altering/removing/etc whatever is needed for the Lite/Full Version.
  2. Manually recompile and change the package name each time you want to release each version.

Either way its a real PITA. Which is why I’m writing this post. If you’re application requires Lite and Full versions (or more) then you’ll love this below.

Android Project Libraries

Android now has project libraries. This solves the problem of above. I now have the following package structure -

  • com.example.myappAndroid Project Library - This is where my ENTIRE app lives. All the functionality for the FULL and LITE versions.
  • com.example.myapp.full - Android Application Project - This is a shell that contains graphics and resources needed for the full version only. Basically it’s a super lightweight shell.
  • com.example.myapp.lite - Android Application Project – This is another shell that contains nothing but graphics and resources needed for the lite version. Again, its a super lightweight shell.

How To Determine Lite vs. Full

In the com.example.myapp, I have an Application object that derives from the Android Application object. In this part of the app I check to see the package name contains the word “lite”. If so, then the app is running under a lite version. Here’s the code:


return getPackageName().toLowerCase().contains("lite");</pre>

Whats Happening Here? The library project takes on the package name of the project that is referencing it. Therefore at runtime, the package name equates to-

com.example.myapp.lite

Therefore I know that I’m running under the context of my LITE app. I can now disable/enable a feature based upon that knowledge.

Conclusion

While this may be a quick and simple approach, it works. Now, inside of my main library (com.example.myapp), I can sprinkle if statements all over the place to determine if the app is the full or lite version. That way I can share a common code base and not have to worry about maintaining different, yet similar code bases.

Downloads

Download a full working example here