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 wich 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);
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.
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:
To open links in the browser you can use an
intent
in theshouldOverrideUrlLoading
method to launch the URL in a browser versus using yourwebview
to handle the link:If you want to load in the webview use: