java – android webview stay in app-ThrowExceptions

Exception or error:

I have a webview in my android app, but when someone navigates around the site, it opens in a new window, i want it to stay inside the webview.. is there a way to do this easily? Here is the code in my activity:

super.onCreate(savedInstanceState);
    setContentView(R.layout.shop);
    WebView webview;
    webview = (WebView) findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("http://www.google.co.uk");
How to solve:

You’ll have to create a WebViewClient:

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

And then set it to your WebView like this:

webview.setWebViewClient(new myWebViewClient());

###

Or you can just do that, without creating a new class:

myWebView.setWebViewClient(new WebViewClient());

It works for me.

###

webview.setWebViewClient(new WebViewClient());

If you want to customize then you should override shouldOverrideUrlLoading (WebView view, String url). but it’s deprecated in API 24. You can use public boolean shouldOverrideUrlLoading (WebView view,WebResourceRequest request). actually both of them needs to return false.

Android WebView Example

Leave a Reply

Your email address will not be published. Required fields are marked *