views:

1195

answers:

2

Hi friends, I have a simple problem, i have loaded an external url in my webview. Now wat i need is that wen the user clicks on the links on the page loaded, it has to work like a normal browser and open the link in the same webview... but its opening the androids default browser and loading the page there???

I have enabled JavaScript... but yet its not working... have i forgotten something???

+11  A: 

If you're using a WebView you'll have to intercept the clicks yourself if you don't want the default Android behaviour.

You can monitor events in a WebView using a WebViewClient. The method you want is shouldOverrideUrlLoading(). This allows you to perform your own action when a particular URL is selected.

You set the WebViewClient of your WebView using the setWebViewClient() method.

If you look at the WebView sample in the SDK there's an example which does just what you want. It's as simple as:

private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}
Dave Webb
thanx dave... just the thing i needed... now its working perfectly fine...
JaVadid
+1  A: 

in some cases you might need an override of onLoadResource if you get a redirect which doesn't trigger the url loading method. in this case i tried the following:

@Override
public void onLoadResource(WebView view, String url)
{
    if (url.equals("http://redirectexample.com"))
    {
        //do your own thing here
    }
    else
    {
        super.onLoadResource(view, url);
    }           
}
realgt