Home » Android » java – "Complete action using" when trying to load in a WebView

java – "Complete action using" when trying to load in a WebView

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m testing the built-in WebView in the Android apps. My problem is that the following code

WebView webView = (WebView) findViewById(R.id.webView1);
webView.loadUrl("http://google.com");

triggers an intent (sugesting the installed browsers for opening the web) instead of open it in the built-in WebView. What should I do for avoiding that?

How to&Answers:
WebView mWebView= (WebView) findViewById(R.id.webView1);
mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            // Handle the error
        }

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

mWebView.loadUrl("http://google.com");

This won’t open other broweser. Have reference here from DEVELOPER’s SITE.

Answer:

You need to implement WebViewClient if you want to open URL within your application.

Check this link and find Handling Page Navigation for the WebViewClient example.