Add a Progress Bar in WebView

前端 未结 8 1721
無奈伤痛
無奈伤痛 2020-11-27 15:50

I am trying to add a progress/loading bar to my application that uses WebView. I am confused on how to implement a progress bar that appears every time a link i

相关标签:
8条回答
  • 2020-11-27 16:39

    pass your url in this method

    private void startWebView(String url) {
    
            WebSettings settings = webView.getSettings();
    
            settings.setJavaScriptEnabled(true);
            webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
    
            webView.getSettings().setBuiltInZoomControls(true);
            webView.getSettings().setUseWideViewPort(true);
            webView.getSettings().setLoadWithOverviewMode(true);
    
            progressDialog = new ProgressDialog(ContestActivity.this);
            progressDialog.setMessage("Loading...");
            progressDialog.show();
    
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
    
                @Override
                public void onPageFinished(WebView view, String url) {
                    if (progressDialog.isShowing()) {
                        progressDialog.dismiss();
                    }
                }
    
                @Override
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    Toast.makeText(ContestActivity.this, "Error:" + description, Toast.LENGTH_SHORT).show();
    
                }
            });
            webView.loadUrl(url);
        }
    
    0 讨论(0)
  • 2020-11-27 16:46

    Put a progress bar and the webview inside a relativelayout and set the properties for the progress bar as follows,

    1. Make its visibility as GONE.
    2. CENTRE it in the Relativelayout.

    and then in onPageStarted() of the webclient make the progress bar visible so that it shows the progressbar when you have clicked on a link. In onPageFinished() make the progress bar visiblility as GONE so that it disappears when the page has finished loading... This will work fine for your scenario. Hope this helps...

    0 讨论(0)
提交回复
热议问题