Milos Cuculovic
Milos Cuculovic

Reputation: 20223

WebView with shouldOverrideUrlLoading and baseURL

I have a WebView on my application and some links which are URL adresses. Some of them don't have the baseURL, only something like "/abc/abc"

I would like to set the baseURL for all the links starting with "/". I have tryed this:

WebView wv = (WebView) findViewById(R.id.EditorialWebView);
wv.setBackgroundColor(0x00000000);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadDataWithBaseURL("", editorialBoard, "text/html", "utf-8", "");
wv.setWebViewClient(new WebViewClient()
    {
@Override
public boolean shouldOverrideUrlLoading(WebView wv, String url) {
if (url.startsWith("/")) 
    {
wv.loadUrl("http://www.mdpi.com"+url);
}  
return true;
}                      
});

But nothing happens.

Upvotes: 1

Views: 1740

Answers (1)

darryn.ten
darryn.ten

Reputation: 6973

Try something like this

myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient() {
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if(url.startsWith("/")) {
      Uri u = Uri.fromParts("http://www.domain.com", url, "");
      view.loadUrl(u);
    }
    return true;
  }
});

Please let me know if it works for you as I am unable to test it at the moment.

Upvotes: 2

Related Questions