1

I'm using android web view to load a url. it's working fine but the problem is some of the links (target=_blank) inside the web page not opening default way. i debug the project and found these links not calling shouldoverrideurlloading() method in webviewclient().

this is webviewclient,

public class MyWebClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;

    }
}
user3800832
  • 421
  • 2
  • 8
  • 22

3 Answers3

3

This is because such links trigger opening of popups (new tabs, if you are in a browser). By default, opening of popup windows is disabled in WebView. Check out http://developer.android.com/reference/android/webkit/WebSettings.html#setSupportMultipleWindows(boolean)

You have to set WebChromeClient for your main WebView, and then provide an empty new WebView which will serve for opening the contents of the popup.

This is the essence of what you need to do:

webView.getSettings().setSupportMultipleWindows(true);

webView.setWebChromeClient(new WebChromeClient {
    @Override
    public boolean onCreateWindow(WebView view, boolean isDialog,
            boolean isUserGesture, Message resultMsg) {
        // Create a WebView
        WebView popupWebView = new WebView(view.getContext());
        // TODO: Put WebView into your view hierarchy, if needed.
        //
        // This is needed to open the url in the WebView.
        // Without the client, WebView will try to start a browser.
        popupWebView.setWebViewClient(new WebViewClient());
        WebView.WebViewTransport transport = 
            (WebView.WebViewTransport) resultMsg.obj;
        transport.setWebView(popupWebView);
        resultMsg.sendToTarget();
        return true;
    }
});

This answer contains more code that is needed if you also want to handle closing of your popup windows: How to handle facebook like with confirm in android webview

BTW, it is not necessary to override shouldOverrideUrlLoading if you just want to open links inside your WebView, simply setting WebViewClient is enough:

webView.setWebViewClient(new WebViewClient());
Community
  • 1
  • 1
Mikhail Naganov
  • 6,643
  • 1
  • 26
  • 26
  • then is it open in browser or web view ? – user3800832 Mar 12 '15 at 04:06
  • @user3800832: That depends on whether you set a WebViewClient for `popupWebView`, and what you do in its `shouldOverrideUrlLoading` (the same thing as for a regular WebView). I added a few more lines into my sample code to illustrate this. – Mikhail Naganov Mar 12 '15 at 10:28
  • in this way external links open within the browser but i want to open those things inside the web view – user3800832 Mar 12 '15 at 13:13
  • @user3800832: `popupWebView.setWebViewClient(new WebViewClient());` does the trick, do you have it? – Mikhail Naganov Mar 12 '15 at 13:24
0

This method is of no use. It will just load the url and always returns a TRUE boolean value.

Try this one

webView.setWebViewClient(new WebViewClient());
webView.getSettings().setDomStorageEnabled(true);
webView.loadUrl("url");
Shubham
  • 165
  • 2
  • 11
0

I found that if you are running in an iframe, shouldOverrideUrlLoading() is not triggered for external links, unless you add target="_top" (I didn't try target="_blank").

The following JavaScript code adds this target attribute to each link.

function change_links_target()
{   
    var all_document_links = mFrameDocument.getElementsByTagName("a");
    for (i = 0; i < all_document_links.length; i++){
        all_document_links[i].setAttribute("target", "_top");
    }
}   
IKavanagh
  • 6,089
  • 11
  • 42
  • 47
Senti
  • 309
  • 2
  • 13