21

I have searched and read a lot of posts but can not figure out how to do it in my code.

I want to use geolocation in my app and need to view in webChromeClient in stead of webViewClient which I use for the html files now and the links does stay in the same view.

When I change this to webChromeClient, the html links, like <a href="http://url/file.php?q=123", are suddenly opening in the browser!

How can I prevent this?

myWebView = new WebView(this);  
myWebView.getSettings().setJavaScriptEnabled(true);   
myWebView.getSettings().setLoadWithOverviewMode(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setGeolocationEnabled(true);
myWebView.setWebChromeClient(new WebChromeClient() {
public void onGeolocationPermissionsShowPrompt(String origin, android.webkit.GeolocationPermissions.Callback callback) { 
        callback.invoke(origin, true, false); }
});
myWebView.loadUrl("file:///android_asset/HTML/index.html");
setContentView(myWebView);
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Harry
  • 786
  • 1
  • 8
  • 27

3 Answers3

80

WebChromeClient doesn't contain the shouldOverrideUrlLoading method, the WebViewClient does. Remember the "WebView" can and does use both WebViewClient and WebChromeClient at the same time if specified. The WebViewClient adds methods not available to you with no client assigned (keeping navigation in the webview). The same with the WebChromeClient has specific methods it can use (get page title on load for example).

So you can build your code like this:

WebView web = (WebView)findViewById(R.id.web);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setGeolocationEnabled(true);
webSettings.setSupportMultipleWindows(true); // This forces ChromeClient enabled.    

web.setWebChromeClient(new WebChromeClient(){
    @Override
    public void onReceivedTitle(WebView view, String title) {
         getWindow().setTitle(title); //Set Activity tile to page title.
    }
});

web.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return false;
    }
});
Chad Adams
  • 1,319
  • 1
  • 9
  • 14
  • 10
    did solve my problem. I assumed (and i guess many other developers) that we should set WebChromeClient OR WebViewClient, but not both. – Bugdr0id Jul 29 '15 at 09:56
  • this help me, i was thinking as said by @Bugdr0id – Asad kamran Mar 31 '16 at 03:59
  • Solved my problem. Before that, I tried http://developer.android.com/guide/webapps/webview.html, but browser still lanches. – Doan Quang Viet Apr 22 '16 at 05:07
  • After much searching, found this answer, and worked. Thanks – Neo Nov 30 '20 at 08:03
  • Took me way too long to find out that you can set both `WebViewClient` and `WebChromeClient` to a `Webview`. Thank you, URL handling within my activity now finally works fine. – ice_chrysler Sep 07 '21 at 07:43
9

I was able to get around this by setting a dummy WebViewClient in addition to the WebChromeClient. Not sure why, but when I take out this line the web page starts opening in the browser again.

mBrowser.setWebViewClient(new WebViewClient());
inky
  • 1,462
  • 14
  • 17
2

To open links in the browser you can use an intent in the shouldOverrideUrlLoading method to launch the URL in a browser versus using your webview to handle the link:

webView.setWebViewClient(new WebViewClient(){
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url != null && url.startsWith("http://")) {
            view.getContext().startActivity(
                new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
        } else {
            return false;
        }
    }
}

If you want to load in the webview use:

WebViewClient yourWebClient = new WebViewClient()
{
   // Override page so it's load on my view only
   @Override
   public boolean shouldOverrideUrlLoading(WebView  view, String  url)
   {
         // This line we let me load only pages with an anchor tag
         if ( url.contains("url") == true )
           //Load new URL Don't override URL Link
        return false;

   // Return true to override url loading (In this case do nothing).
   return true;
    }
};
Nick
  • 9,285
  • 33
  • 104
  • 147
  • Sorry, but that is the contrary. I want the links, like – Harry Feb 02 '13 at 22:19
  • 5
    Thanks for trying to help Nick but I lost you... You are coding for WebViewClient and I am asking about WebChromeClient... – Harry Feb 03 '13 at 10:21