How to stop java code execution util a link in webView is loaded completely in android studio

73 views Asked by At

I am trying to make a web scraping application in android studio, in which on extracted data I want to perform some action using java code. Problem is that webview works asynchronously that is, it will go on executing remaining line of same method along with loading website in webview. But my code after webview.url call needs data which is only available when site is completely loaded, so I want java to wait, for site to completely load then execute remaining line of code. Any suggestion is appreciated

1

There are 1 answers

1
Ramy Ibrahim On
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new MyWebViewClient());

progress = (ProgressBar) findViewById(R.id.progressBar);
progress.setVisibility(View.GONE);

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

     @Override
    public void onPageFinished(WebView view, String url) {
         progress.setVisibility(View.GONE);             
        super.onPageFinished(view, url);
 // Here webview for finished loading
    }

     @Override
     public void onPageStarted(WebView view, String url, 
 Bitmap favicon) {
          progress.setVisibility(View.VISIBLE);          
        super.onPageStarted(view, url, favicon);
    }
 }