Reputation: 6754
I am loading the content of a locally stored HTML
file in a WebView
in my app. This file also contains links inside it, which when clicked, load the URLs
in the same WebView
. I want to load these links in an external browser instead. I have taken a look at onPageStarted
and shouldOverrideUrlLoading
, but they don't seem to work. I have tried the following:
webAbout = (WebView) findViewById(R.id.wvAbout);
webAbout.loadUrl(Const.defURL);
webAbout.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
if (!url.startsWith(Const.defURL)) {
view.stopLoading();
// DO SOMETHING
}else{
view.loadUrl(Const.defURL);
}
}
});
I have also tried:
webAbout = (WebView) findViewById(R.id.wvAbout);
webAbout.loadUrl(Const.defURL);
webAbout.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && (url.startsWith(Const.defURL))){
webAbout.loadUrl(Const.defURL);
return true;
}
else {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return false;
}
}
});
but in both cases, the WebView
does not load anything. Any idea where I am going wrong?
Upvotes: 2
Views: 2230
Reputation: 3231
Return false
from shouldOverrideUrlLoading
when you want the URL loaded in the WebView, return true
if you don't want the WebView to load the URL (that means you've somehow handled it yourself).
Remember that:
WebViewClient
before calling loadUrl
,shouldOverrideUrlLoading
is not called for the URL that's passed to loadUrl
.The code you had should work with some modifications:
webAbout.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && (url.startsWith(Const.defURL))){
return false;
} else {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
}
});
// I'm assuming Const.defURL doesn't need escaping.
String customHtml = "<html><body><a href=\"" + Const.defURL + "\">Load url in webview</a><a href=\"http://stackoverflow.com/\">load in browser</a></body></html>";
// Here you would normally load your page using loadUrl,
webAbout.loadData(customHtml, "text/html", "UTF-8");
Edit: The method boolean shouldOverrideUrlLoading(WebView, String)
is marked as deprecated and the documentation recommends using boolean shouldOverrideUrlLoading(WebView, WebRequest)
instead. But, the new method is never called. You still have to override boolean shouldOverrideUrlLoading(WebView, String)
.
Upvotes: 2
Reputation: 12725
Change your second method to this:
webAbout = (WebView) findViewById(R.id.wvAbout);
webAbout.loadUrl(Const.defURL);
webAbout.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && (url.startsWith(Const.defURL))){
view.loadUrl(url); //use new url instead of const.defURL
return true;
} else {
//use browser
return super.shouldOverrideUrlLoading(view, url);
}
}
});
taken from https://stackoverflow.com/a/8541631/2829009
Upvotes: 0
Reputation: 121
Just try this
webView=(WebView)findViewById(R.id.wvAbout);
String url="";
webView.loadUrl(url);
or write this condition
if (urlPath
.compareToIgnoreCase(Const.defURL) == 0) {
} else {
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.clearView();
view.loadUrl(url);
return true;
}
});
}
Upvotes: 0