Add a Progress Bar in WebView

前端 未结 8 1718
無奈伤痛
無奈伤痛 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:20

    You can try this code into your activity

        private void startWebView(WebView webView,String url) {
     webView.setWebViewClient(new WebViewClient() {
                ProgressDialog progressDialog;
    
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return false;
                }
    
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
                }
    
                public void onLoadResource (WebView view, String url) {
    
                        if (progressDialog == null) {
                            progressDialog = new ProgressDialog(SponceredDetailsActivity.this);
                            progressDialog.setMessage("Loading...");
                            progressDialog.show();
                        }
    
                }
                public void onPageFinished(WebView view, String url) {
                    try{
                        if (progressDialog.isShowing()) {
                            progressDialog.dismiss();
                            progressDialog = null;
                        }
    
                    }catch(Exception exception){
                        exception.printStackTrace();
                    }
                }
    
            });
    
            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl(url);
        }
    

    Call this method using this way:

    startWebView(web_view,"Your Url");
    

    Sometimes if URL is dead it will redirected and it will come to onLoadResource() before onPageFinished method. For this reason progress bar will not dismis. To solve this issue see my this Answer.

    Thanks :)

    0 讨论(0)
  • 2020-11-27 16:22

    Here is the code that I am using:

    Inside WebViewClient:

                   @Override
                 public void onPageStarted(WebView view, String url, Bitmap favicon) {
    
                  super.onPageStarted(view, url, favicon);
                  findViewById(R.id.progress1).setVisibility(View.VISIBLE);
                 }
    
                @Override
                public void onPageFinished(WebView view, String url) {
                    findViewById(R.id.progress1).setVisibility(View.GONE);
                }
    

    Here is the XML :

    <ProgressBar
        android:id="@+id/progress1"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    

    Hope this helps..

    0 讨论(0)
  • 2020-11-27 16:22

    The best approch which worked for me is

    webView.setWebViewClient(new WebViewClient() {
    
        @Override public void onPageStarted(WebView view, String url, Bitmap favicon) {
              super.onPageStarted(view, url, favicon);
              mProgressBar.setVisibility(ProgressBar.VISIBLE);
              webView.setVisibility(View.INVISIBLE);
            }
    
        @Override public void onPageCommitVisible(WebView view, String url) {
          super.onPageCommitVisible(view, url);
          mProgressBar.setVisibility(ProgressBar.GONE);
          webView.setVisibility(View.VISIBLE);
          isWebViewLoadingFirstPage=false;
        }
    }
    
    0 讨论(0)
  • 2020-11-27 16:28

    I try dismis progress on method onPageFinished(), but not good too much, it has time delay to render webview.

    try with onPageCommitVisible() better:

    val progressBar = ProgressDialog(context)
        progressBar.setCancelable(false)
        progressBar.show()
        val url = "your url here"
        web_container.settings.javaScriptEnabled = true
        web_container.loadUrl(url)
    
        web_container.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                view.loadUrl(url)
                progressBar.show()
                return true
            }
    
            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
            }
            override fun onPageCommitVisible(view: WebView?, url: String?) {
                super.onPageCommitVisible(view, url)
                progressBar.dismiss()
            }
        }
        web_container.setOnKeyListener(View.OnKeyListener { _, keyCode, event ->
            if (keyCode == KEYCODE_BACK && event.action == MotionEvent.ACTION_UP
                    && web_container.canGoBack()) {
                web_container.goBack()
                return@OnKeyListener true
            }
            return@OnKeyListener false
        })
    
    0 讨论(0)
  • 2020-11-27 16:35

    I have added few lines in your code and now its working fine with progress bar.

            getWindow().requestFeature(Window.FEATURE_PROGRESS);
            setContentView(R.layout.main );
            // Makes Progress bar Visible
            getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
    
            webview = (WebView) findViewById(R.id.webview);
            webview.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress)   
                {
                    //Make the bar disappear after URL is loaded, and changes string to Loading...
                    setTitle("Loading...");
                    setProgress(progress * 100); //Make the bar disappear after URL is loaded
         
                    // Return the app name after finish loading
                    if(progress == 100)
                       setTitle(R.string.app_name);
                    }
                });
            webview.setWebViewClient(new HelloWebViewClient());
            webview.getSettings().setJavaScriptEnabled(true);
            webview.loadUrl("http://www.google.com");
    
    0 讨论(0)
  • 2020-11-27 16:35

    in oncreate method where you have set your Webview.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.web_view);
    
    web_view = (WebView) findViewById(R.id.web_view);
    pd = new ProgressDialog(SiteOpenInWebView.this);
        pd.setMessage("Please wait Loading...");
        pd.show();
        web_view.setWebViewClient(new MyWebViewClient());
        web_view.loadUrl("ur site name");
     }
    

    WebViewClient

    private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
    
        if (!pd.isShowing()) {
            pd.show();
        }
    
        return true;
    }
    
    @Override
    public void onPageFinished(WebView view, String url) {
        System.out.println("on finish");
        if (pd.isShowing()) {
            pd.dismiss();
        }
    
      }
    }
    
    0 讨论(0)
提交回复
热议问题