Thursday, May 26, 2011

Installing the ChildBrowser Plugin on PhoneGap Android

I'm writing up a quick tutorial on how to get the ChildBrowser plugin for PhoneGap Android installed as a few folks on the mailing list are having troubles getting it going. First lets start off with a stock PhoneGap Android project in Eclipse:


Step one is to create the directory structure for the ChildBrowser.java file. Click on the src folder then right mouse click or control click on the folder.  From the popup menu select New and Package.


In the dialog box that appears type in com.phonegap.plugins.childBrowser then click the Finish button. Please note the package name is case sensitive so make sure it matches exactly.


Step two is adding the ChildBrowser.java class to your project.  Download the file from this link and save it in your projects src/com/phonegap/plugins/childBrowser directory.  Then click on you projects name then right mouse click and select Refresh.  Your project explorer should now look like this:

You shouldn't see any red exclamation marks at this point.  If you do you've probably named your package incorrectly.

Step three is to add the childbrowser.js file to your project.  Download the file from this link and save it in your projects assets/www directory. Then click on you projects name then right mouse click and select Refresh. Your project explorer should now look like this:


In step four we will need to modify the AndroidManifest.xml file to add an intent filter for the ChildBrowser.  Paste the following lines inside the <application/> tag:

<activity android:name="com.phonegap.DroidGap" android:label="@string/app_name">
<intent-filter>
</intent-filter>
</activity>

Here is a gist of what my AndroidManifest.xml looks like.

In step five we need to modify our index.html file to a reference the childbrowser.js and call the ChildBrowser.  In the <head/> tag after the reference to phonegap.0.9.5.js add the following line:

<script type="text/javascript" charset="utf-8" src="childbrowser.js"></script>

and somewhere in your <body/> tag add a line like this:

<a href="#" onclick="window.plugins.childBrowser.showWebPage('http://www.google.com');">Open Google.com in child browser</a>

This will give you an app that shows you this:


and opens Google in the ChildBrowser when you click on the link.


The full gist of my index.html is available as well.

Hope this helps everyone!

21 comments:

Antoine said...

Hello,

I was following your tutorial but on clicking on the link nothing happens.
In logCat I can see those errors:
-ReferenceError: Can't find variable: PhoneGap in childbrowser.js
-TypeError: Result of expression 'windows.plugins' [undefined]

So I suppose that linking with the plugManager is not working.
Any Idea?

Simon Mac Donald said...

Are you sure your child browser.js comes after the PhoneGap.js? Also what version of PhoneGap are you using?

skogsmaskin said...

I have the same problem with PhoneGap 0.9.5.1.

Nothing happens. Can't see any errors either.

Tobias said...

Hi,
when I run the App i recive the error:
Error: Status=2 Message:Class not found at file...

using phonegap 0.9.5.1

Simon Mac Donald said...

@Tobias I need some more info than that to figure out what is going on. You are probably better off taking this type of discussion over to the PhoneGap Google Group.

Cory said...

Thank you very much for this tutorial!

White said...

I haved the same problem : nothing happens!

And I found that in Build Path->Source->com.phonegap.plugins.childBrowser are excluded !

casper60 said...

Is the ChildBrowser on Android supposed to show the native addressbar? Im not sure how this works on Android, but on the iPhone version it doesn't show the default address bar in the top. I can't find any video of how it is supposed to behave, so I'm not sure if its intentional.

I have uploaded som images here:

http://art-journal.dk/work/screen1.png
http://art-journal.dk/work/screen2.png
http://art-journal.dk/work/screen3.png

Any help would be much appreciated.

Kind regards,
Casper Knudsen - Art-journal.dk

Simon Mac Donald said...

Hey Casper, yes, the address bar on top of the ChildBrowser is intentional. The Android and iOS versions are not exactly the same.

Mahendra said...

Hi Simon,

Can you show an example of using the ChildBrowser plugin to post on Facebook wall in Android. There is an example for FBConnect in iOS, but not for Android.

Any help in this will be highly appreciated.

Thanks.

Simon Mac Donald said...

@Mahendra look for the FBConnect plugin as it will do what you want. Dave Johnson of Nitobi demoed it at PhoneGap day last Friday.

Reza Lesmana said...

Hi, Simon

thanks for this tutorial. And also the tutorial on the installing PhoneGap 1.0. However, I found out that I need to use a second argument (boolean true) for showWebPage if I want it to show it on the PhoneGap app directly. At least for the latest ChildBrowser plugin it needs to use it. Thanks. :)

kumarpsk said...

Hello ,

How to I call the android activity through the phonegap javascript, it is possible or not ,if possible means please give me a solution


thanking you
kumarp

Simon Mac Donald said...

@kumarspk

This is a question best posted on the PhoneGap Google Groups but if you are looking to call an intent from JS you can use the Web Intent plugin.

kumarpsk said...

sir,
One more question How to i call android java class method using javascript in phonegap

Simon Mac Donald said...

Read up on how to write a plugin.

Robert (aka Defused) said...

So please, can anybody help, how to hide address bar in ChildBrowser?

Jithin said...

Hai Simon MAc,



Thanks for the coding...
I used this one, but it is working fine in emulator, it is not working inmy samsung galaxy pop.... what is the problem?

Simon Mac Donald said...

@Jithin

Need some more details.

Mitch said...

How did you get the ChildBrowser window to appear full screen vs. as a modal dialog?

Simon Mac Donald said...

@Mitch

If you use showWebPage it uses the modal dialog with an embedded web view. If you use openExternal it starts a separate browser using an intent.