Hide address bar in Android webview

4,268 views
Skip to first unread message

Josiah

unread,
May 5, 2009, 5:41:44 PM5/5/09
to phonegap
I've setup my Android app just like the Wiki setup directions using
the latest PhoneGap build. When I launch the app in the Android
emulator no address bar is present on the first screen. After I
follow a link on the page
to a second screen, a new window launches and an address bar appears
on the screen.

I would like to know how to disable that address bar in the Webview.
Anyone have any ideas?

Josiah

unread,
May 5, 2009, 6:09:49 PM5/5/09
to phonegap
I figure since this seems to influence everyone who uses PhoneGap
Android this way, it may be of benefit to more than just me. Where
Android gives away the URL of the webapp, it seems to undermine the
efforts to make the app native to begin with.

Ray

unread,
May 6, 2009, 12:25:07 AM5/6/09
to phonegap
Hi Josiah,

this is something I haven't got around to contributing back yet:

To keep future url clicks in the same window and not use the phones
default browser, you need to use webviewclient

In the main class file before the bindBrowser(appView); add the
following:
Note that the error page is optional but I think it's very worthwhile
including, you will need to add the errorpage.txt file to your project
assets directory.

/* WebViewClient prevents remote urls from opening a new
browser window */
appView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl)
{
appView.stopLoading();
appView.loadUrl("file:///android_asset/errorpage.txt");
}
});


/* Bind the appView object to the gap class methods */
bindBrowser(appView);

Cheers
Ray

Twitch

unread,
May 6, 2009, 1:17:11 AM5/6/09
to phonegap
This is exactly what I'm looking to do with my iPhone app. How does
one accomplish this same thing for the iPhone?

Thanks in advance,
Twitch
> > > emulator noaddressbaris present on the first screen.  After I
> > > follow a link on the page
> > > to a second screen, a new window launches and anaddressbarappears
> > > on the screen.
>
> > > I would like to know how to disable thataddressbarin the Webview.
> > > Anyone have any ideas?

Ray

unread,
May 6, 2009, 2:28:22 AM5/6/09
to phonegap
I guess you could just remove

[[UIApplication sharedApplication] openURL:url];

which is in the PhonegapDelegateApp.m file
/*
* We don't have a PhoneGap request, it could be file or something
else
*/
/* warning, something else may stop working because this was removed
*/
////if (range.location == NSNotFound) {
////[[UIApplication sharedApplication] openURL:url];
////}

Twitch

unread,
May 6, 2009, 2:46:19 AM5/6/09
to phonegap
Thanks Ray, I'll take a look at it in the morning when I'm a little
fresher. My eyes are crossing..haha

Josiah

unread,
May 6, 2009, 6:53:11 PM5/6/09
to phonegap
Ray ~

Your code and explanation saved the day.

Thanks!

-Josiah

Twitch

unread,
May 6, 2009, 11:39:30 PM5/6/09
to phonegap
Spot on Ray! That did the trick! Whole new ball game now. Can't
thank you enough.

-Twitc

On May 6, 1:28 am, Ray <rnva...@googlemail.com> wrote:

PanMan

unread,
May 18, 2009, 10:11:39 AM5/18/09
to phonegap
Hi Joshiah!
Is it possible to have some links be launched by their native apps?
Longer story:
I was looking for this, as I have something similar on my iPhone app.
However, when I link to a (external) video file on the iphone, it's
still handled by the video player on the OS. On Android this is not
the case: Without the patch above, it launches the browser, which
launches the video player (but if you go back you end up in the
browser, not the original app). If I apply this code above, no links
ever open outside the app (good!), but also the video files won't play
(bad), most likely because the browser itself can't handle these.
Any idea how to fix this? Is it possible to detect the file type, or
url, and have that handled by external programs, while keeping other
links in the app?

(my video links have a redirect, added by our loadbalancers.).

Thanks a lot!
PanMan.

On May 6, 6:25 am, Ray <rnva...@googlemail.com> wrote:
> Hi Josiah,
>
> this is something I haven't got around to contributing back yet:
>
> To keep future url clicks in the same window and not use the phones
> default browser, you need to use webviewclient
>
> In the main class file before the bindBrowser(appView); add the
> following:
> Note that the error page is optional but I think it's very worthwhile
> including, you will need to add the errorpage.txt file to your project
> assets directory.
>
>         /* WebViewClient prevents remote urls from opening a new
> browser window */
>         appView.setWebViewClient(new WebViewClient() {
>                 @Override
>                 public void onReceivedError(WebViewview, int errorCode,
> String description, String failingUrl)
>                 {
>                         appView.stopLoading();
>                         appView.loadUrl("file:///android_asset/errorpage.txt");
>                 }
>         });
>
>         /* Bind the appView object to the gap class methods */
>         bindBrowser(appView);
>
> Cheers
> Ray
>
> On May 6, 5:09 am, Josiah <josiahlcarl...@gmail.com> wrote:
>
> > I figure since this seems to influence everyone who uses PhoneGap
> >Androidthis way, it may be of benefit to more than just me.  Where
> >Androidgives away the URL of the webapp, it seems to undermine the
> > efforts to make the app native to begin with.
>
> > On May 5, 3:41 pm, Josiah <josiahlcarl...@gmail.com> wrote:
>
> > > I've setup myAndroidapp just like the Wiki setup directions using

Ray

unread,
May 19, 2009, 2:52:03 AM5/19/09
to phonegap
Hi PanMan,

There was a similar question recently for iPhone in another thread
http://groups.google.com/group/phonegap/browse_thread/thread/b7d5f1c2393dd1ec/3bcc60513b7caa87?hl=en&lnk=gst&q=some+links+inside#3bcc60513b7caa87

in the case of Android the way I would do it is to add another
override for webViewClient (as above for onRecievedError, but this
time it's shouldOverrideUrlLoading)

@Override
public boolean shouldOverrideUrlLoading(WebView appView, String url)
{
// this is just an example, you could match whatever url you want
to, and do whatever you want to do once it's matched
if (url.contains("reboot.org.uk")) {
// plug Ray's new blog :)
// call whatever you want here


return false; // returning false means you don't go on to
load the page, returning true means you do
}
}

This happens to be the same way iPhone PhoneGap works (in iPhones case
overriding urls that start with gap://). Interestingly we could use
the above method to have completely the same gap.js for android as
iPhone.

Ray

unread,
May 20, 2009, 5:23:21 AM5/20/09
to phonegap
as h@nes asked in another thread, and I just pointed him/her here

if (// call whatever you want here) is to open the default web browser
at a url you can do it like so:

Intent myIntent = null;
try {
myIntent = new Intent("android.intent.action.VIEW",
Uri.parse("http://reboot.org.uk")); // more shameless advertising :)
Use 'url' to get the intercepted URL
} catch (Exception e) {
e.printStackTrace();
}
startActivity(myIntent);
}

On May 19, 1:52 pm, Ray <rnva...@googlemail.com> wrote:
> Hi PanMan,
>
> There was a similar question recently for iPhone in another threadhttp://groups.google.com/group/phonegap/browse_thread/thread/b7d5f1c2...
>
> in the case of Android the way I would do it is to add another
> override for webViewClient (as above for onRecievedError, but this
> time it's shouldOverrideUrlLoading)
>
> @Override
> public boolean shouldOverrideUrlLoading(WebView appView, String url)
> {
>      // this is just an example, you could match whatever url you want
> to, and do whatever you want to do once it's matched
>      if (url.contains("reboot.org.uk")) {
>         // plugRay'snew blog :)
Reply all
Reply to author
Forward
0 new messages