Issue
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("http://google.com");
This is my code for webview. It works fine but when a link is clicked it opens the default browser and does not load the link in webview.
Help me out!
Solution
You need to implement your own WebViewClient to tell the webView wich urls or domanis should handle. Something like this:
private class BaseWebViewClient extends WebViewClient {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (Uri.parse(request.getUrl().toString()).getHost().contains("yourdomain.com.ar")) {
callback.setWebView(view);
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(request.getUrl().toString()));
startActivity(intent);
return true;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().contains("yourdomain.com.ar")) {
callback.setWebView(webView);
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Then set it to your webView element:
webView.setWebViewClient(new BaseWebViewClient());
Answered By - Jonathan Aste
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.