Skip to content Skip to sidebar Skip to footer

Webchromeclient Opens Link In Browser

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 webViewC

Solution 1:

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(newWebChromeClient(){
    @OverridepublicvoidonReceivedTitle(WebView view, String title) {
         getWindow().setTitle(title); //Set Activity tile to page title.
    }
});

web.setWebViewClient(newWebViewClient() {
    @OverridepublicbooleanshouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        returnfalse;
    }
});

Solution 2:

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(newWebViewClient());

Solution 3:

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(newWebViewClient(){
    publicbooleanshouldOverrideUrlLoading(WebView view, String url) {
        if (url != null && url.startsWith("http://")) {
            view.getContext().startActivity(
                newIntent(Intent.ACTION_VIEW, Uri.parse(url)));
            returntrue;
        } else {
            returnfalse;
        }
    }
}

If you want to load in the webview use:

WebViewClient yourWebClient = newWebViewClient()
{
   // Override page so it's load on my view only@OverridepublicbooleanshouldOverrideUrlLoading(WebView  view, String  url)
   {
         // This line we let me load only pages with an anchor tagif ( url.contains("url") == true )
           //Load new URL Don't override URL Linkreturnfalse;

   // Return true to override url loading (In this case do nothing).returntrue;
    }
};

Post a Comment for "Webchromeclient Opens Link In Browser"