Search code examples
androidandroid-studiowebviewandroid-webview

Open External Links inside Webview


I want to open a webview with POST data. So I am doing this:

webView = (WebView) findViewById(R.id.dashboard);
String url = "http://www.example.test";
String postData = "json=" + JSON;
webView.postUrl(url, postData.getBytes());

So now when I launch this webview, clicking on links open a default browser of device, is there any way to stick to webview for opening links?
I researched but all of them are for GET requests.


Solution

  • So i found the solution it is pretty much same for both GET and POST requests.

        webView = (WebView) findViewById(R.id.dashboard);
    
        String url = "http://www.example.test";
        String postData = "json=" + JSON;
    
        webView.postUrl(url,postData.getBytes());
    
        webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView viewx, String urlx) {
                viewx.loadUrl(urlx);
                return false;
            }
        });