Monday, November 12, 2012

Building a Native Context Menu for PhoneGap Android

So I know I promised that I'd write about a HTML only menu but I thought folks might be interested in native context menus as well. It's not much different than my last blog post on menu's so it should be easy to follow.

So when I talk about a native context menu I mean the menu that clicks when you do a long press on an Android device. Again this is well covered on the Android Developer page. Here is an example of an context menu from the Contacts app:


The first step in creating the menu is to define it in XML. My recommendation is to create a menu folder under the res folder in your Android Eclipse project. Then you create a new File with an XML extension. For this post I'll call mine example.xml. When you're done your res folder should look something like this:


Don't worry about the red X's as we haven't edited the XML file yet so it is going to complain about a premature end of file. For a real good description of what goes into a menu XML file read this. For our purposes I'm just going to give you the XML to paste into the file:

But when you view the file in Eclipse you'll still see errors:


and that is to be expected as we haven't added the two images to the drawables folder nor have we added our two new strings to strings.xml. To fix these issues first download these two images:


and copy them to your projects res/drawable-hdpi folder. When you want to put out a professional application you'll need to provide icons of various sizes but the above is enough for our needs.

To fix the rest of the red X's you'll need to open the res/values/strings.xml and add two new Strings. The first should have the name "settings" and a value of "Settings. The second should have the name"help" and, predictably, the value of "Help". At this point all the red X's from example.xml and from the res folder hierarchy should have disappeared. 


If they haven't, well then you've done something wrong and should probably go back over all the steps to make sure you haven't missed anything.

Okay, so now we've got all the resources in place. Let's go write some actual Java code. Don't worry much of it will be cut and paste. In Eclipse open up your main Java class, you know the one that extends DroidGap.

First you'll need to register the context menu in the onCreate method. Basically, you'll need to add the call to registerForContextMenu to your onCreate method:


Secondly we'll need to tell your application what to do when it detects a long press. You'll need to paste the following code into you main Java class:

You shouldn't need to change anything unless you've called your menu XML file something different than example.xml. If you have you'll need to change R.menu.example into R.menu. where you replace with the name of your file minus the .xml extension.

The last bit of code you need to add is a method that will tell your application what to do when the individual menu items are clicked. Here's some example code:


When you click on the Setting button we'll use an Android intent to load the devices Settings panel. If you click on the Help button then we'll execute some JavaScript code. That's it! You don't need to add anything to your HTML/JS code to get a native Android context menu working with your PhoneGap application. Here is what it would look like:



Please note, I'm just providing a quick example and you are not limited to how you handle the menu button clicks in the least. You may want to handle it in Java and do something that you can't do in JavaScript or you may want to call a JavaScript function on your page to have your UI react in some interesting way.

So, there you go. The process is nearly identical to creating an options menu that I described in my previous post. I promise that in my next post I'll show you how to handle everything in HTML and JavaScript!

Friday, November 9, 2012

Books I've Read This Week

The Happiness Project by Gretchen Rubin  is not the usual type of book I read. Honestly, I have a lot of things to be thankful for. I've got my health, I'm married to an amazing woman, we have a great child, I'm gainfully employed in a job that I actually enjoy but I'm not sure you could call me a happy person. Ernest Hemmingway once said "Happiness in intelligent people is the rarest thing I know." and I identify with that statement. My mind is always running thinking of ways to make things better or thinking of what else needs to be done.

So, that's why I was attracted to the Happiness Project. I enjoyed reading the book and what I've really taken from it is just to let go of things. I lead an incredibly blessed life and I should not let the little things bother me quite so much. The book is quite well written in a light tone. It's organized in months where each month focuses on a particular way to improve one's happiness. This makes a lot of sense as it is hard to make new positive habits if you are trying to focus on too many things.



Thursday, November 8, 2012

My PhoneGap Plugins Have Moved

If you are looking for any of the plugins that I have contributed to the PhoneGap Plugins repo they have moved into my own repo. Why do you ask? Well you should go read Shaz's blog post as to the why's and wherefore's.

So you can now find my plugins at:

Tuesday, November 6, 2012

Building a Native Menu with PhoneGap Android

So this question has been posed to me quite a few times over the past couple of weeks so I figure this is probably a good idea for a series of blog posts. In this instalment I'll talk about creating a native menu and next time we'll use only HTML.

So when I talk about a native menu I mean an Android options menu and a lot of the information I'm going to give you is covered very well on this Android Developer page but what I'm going to do is help show you where to add the hooks into your PhoneGap application. Here is an example of an options menu from the Contacts app:


The first step in creating the menu is to define it in XML. My recommendation is to create a menu folder under the res folder in your Android Eclipse project. Then you create a new File with an XML extension. For this post I'll call mine example.xml. When your're done your res folder should look something like this:


Don't worry about the red X's as we haven't edited the XML file yet so it is going to complain about a premature end of file. For a real good description of what goes into a menu XML file read this. For our purposes I'm just going to give you the XML to paste into the file:

But when you view the file in Eclipse you'll still see errors:


and that is to be expected as we haven't added the two images to the drawables folder nor have we added our two new strings to strings.xml. To fix these issues first download these two images:


and copy them to your projects res/drawable-hdpi folder. When you want to put out a professional application you'll need to provide icons of various sizes but the above is enough for our needs.

To fix the rest of the red X's you'll need to open the res/values/strings.xml and add two new Strings. The first should have the name "settings" and a value of "Settings. The second should have the name"help" and, predictably, the value of "Help". At this point all the red X's from example.xml and from the res folder hierarchy should have disappeared. 


If they haven't, well then you've done something wrong and should probably go back over all the steps to make sure you haven't missed anything.

Okay, so now we've got all the resources in place. Let's go write some actual Java code. Don't worry much of it will be cut and paste. In Eclipse open up your main Java class, you know the one that extends DroidGap.

First we'll need to tell your application what to do when it detects that the menu button has been pressed. You'll need to paste the following code into you main Java class:

You shouldn't need to change anything unless you've called your menu XML file something different than example.xml. If you have you'll need to change R.menu.example into R.menu. where you replace with the name of your file minus the .xml extension.

The second bit of code you need to add is a method that will tell your application what to do when the individual menu items are clicked. Here's some example code:


When you click on the Setting button we'll use an Android intent to load the devices Settings panel. If you click on the Help button then we'll execute some JavaScript code. That's it! You don't need to add anything to your HTML/JS code to get a native Android options menu working with your PhoneGap application. Here is what it would look like:



Please note, I'm just providing a quick example and you are not limited to how you handle the menu button clicks in the least. You may want to handle it in Java and do something that you can't do in JavaScript or you may want to call a JavaScript function on your page to have your UI react in some interesting way.

In my next post I'll show you the reverse. How to handle everything in HTML and JavaScript!

Monday, November 5, 2012

My WD MyBook Live Crashed

Well, we had a bit of a power hiccup last week and when I got home my WD MyBook Live could no longer be recognized on my home network. Nothing I could do would get the drive working again so I had to assume the power outage fried the ethernet chip on the board of the enclosure. Googling around the internet showed other folks encountered the same problem that I have.

This is the second time a WD enclosure has failed on me. Why, oh why, do I do this to myself. I guess it is because I'm cheap and the WD products offer a good price/functionality ratio. However, what they do not offer is reliability. This is a good lesson for us all not to skimp on quality when you have an important piece of your home setup.

I guess I should explain that I use the MyBook Live to host my iTunes library. As a result it is just packed with shows for my daughter and I really need to get this fixed before she wants to watch Beverly Hills Chihuahua again. As well this drive is setup to be the receptacle of the Time Machine backup on the Mac. I get antsy if my backup isn't running daily even though I also run Carbonite to backup to the cloud.

Anyway, the drive in the enclosure is just fine. I pulled it out and popped it into SATA to USB which works great except for the fact the drive is formatted as XFS. It's a typical Linux file system which predictably isn't recognized by Mac OS or Windows. Luckily there is a great free program called Linux Reader from Disk Internals that you can run on Windows which will allow you read only access to the partitions. I've got it setup on an old Windows laptop now and it is busily copying away.

Friday, November 2, 2012

Support Canadian Comic Book Creators

Extremely talented Canadian comic book creators have put together an IndiGoGo campaign to self publish a 100 page hard cover anthology of comic stories. They are looking to raise $20,000 by November the 5th and are currently just shy of $13,000. If you love comics then this will be a great anthology to pick up. There are perks levels from $5 to $300 so they are making it so easy for you to find a contribution level you are comfortable with.

Who are they? Well there is...

Adrian Alphona


Andy Belanger



J. Bone


Jack Briglio


Scott Chantler

Tom Fowler

Agnes Garbowska she drew one of my childhood favourites Spider-Ham but I can't get the image to load.

Faith Erin Hicks I recently read all her books.
Tim Levins
Ramon Perez
Ron Salas
Jay Stephens
J. Torres
Howard Wong

I will add images for the rest of these folks as I have time but really you shouldn't need anymore convincing. Go contribute!

Books I've Read This Week

This weeks entry is a bit of an amuse-bouche as I needed to read something light between some of the weightier books I've got on the go.

Invincible Iron Man - Volume 2: World's Most Wanted - Book 1 by Matt Fraction was a bit of a mistake. I meant to get volume 1 of this run by Fraction but accidentally got volume 2 so there is no going back now! Even though I had not read the first volume which collects the first 7 issues of this comic series I was not lost upon starting to read this volume.

We pick up at a point where Iron Man/Tony Stark has been fired from his job of Director of S.H.E.I.L.D. Basically, the top cop in the world. Additionally, his multi-billion dollar company, Stark Industries, is failing and Tony has to go on the run to protect the secrets in his head from the worlds new top cop and former/current super villain Norman Osborn.

His audacious plan to protect his secrets includes erasing his memory. Dun-dun-dah!
Invincible Iron Man - Volume 3: World's Most Wanted - Book 2 by Matt Fraction picks up immediately where the last volume left off. There is a pretty interesting plot point that is continued on in this volume. As Tony continues to lose bits of his memory he has to move down the technology ladder to earlier Iron Man suits as he does not have the mental faculties to operate the more advanced armours anymore. Which is a real problem as the threats he face get exponentially more difficult to over come.

Anyway, at the end of this volume Tony is left at, arguably, the lowest point of his career. Volume 4 should be an interesting read to see how they put Tony Stark back together again.

Oh, and Salvador Larroca is the artist on both volumes. I think he is a great artist but I would be remiss if I didn't mention that some of the photo-realistic techniques he used in the book took me out of the story. It was a bit of an uncanny valley situation where the characters looked too real but not real enough. I really don't mind if my comic books look like comic books or even cartoony.