Search code examples
androidcssurlandroid-intentandroid-browser

How to load a new Window using HTML url? in Android


I have a HTML property to load

<a href="https://www.w3schools.com" target="_blank" rel="noopener noreferrer">Visit W3Schools.com!</a>

I load this to my Android Screen using

webView.loadDataWithBaseURL(null, my_url, "text/html; charset=utf-8", "UTF-8", null);

But when I try clicking on it the link doesn't take me to a new window but opens in the current window, even though i specified target="_blank" in my HTML.

How can i make it load the link in a new window in Android??


Solution

  • Found the answer in this link Android - Open target _blank links in WebView with external browser

    To Summarize:

        wv.getSettings().setSupportMultipleWindows(true);
    wv.setWebChromeClient(new WebChromeClient() {
        @Override
        public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg)
        {
            WebView.HitTestResult result = view.getHitTestResult();
            String data = result.getExtra();
            Context context = view.getContext();
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(data));
            context.startActivity(browserIntent);
            return false;
        }
    });