Show a progress bar on a child tab until the WebView loads

前端 未结 4 1135
小鲜肉
小鲜肉 2021-01-01 00:05

In an Android app I am using a TabView and one of the tabs shows a WebView. But the page is blank until the web page loads. How would one show a progress bar until the page

相关标签:
4条回答
  • 2021-01-01 00:32

    After initializing your view just call this method

    startWebView(web_view,"Your Url"); Here web_view is your initialized WebView Object. The startWebView() is given below:

        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);
        }
    

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

    Thanks :)

    0 讨论(0)
  • 2021-01-01 00:34

    There's a really good tutorial on the Android Developers website for that. It shows how to create the 'spinning wheel' progress dialog used throughout Android programs, and even some basics on how to handle loading in a separate thread to prevent your application from freezing while loading.

    0 讨论(0)
  • 2021-01-01 00:38

    If your question is "how do I find out when the page is loaded?", then:

    Create a custom subclass of WebViewClient, overriding onPageFinished()

    Attach an instance of your WebViewClient subclass to your WebView via setWebViewClient()

    Set up the indefinite progress indicator (bar, dialog, RotateAnimation, etc.)

    progressDialog = ProgressDialog.show(this, "", getText(R.string.progressDialogText), true);
    

    before calling loadUrl() on the WebView

    Have onPageFinished() get rid of the progress indicator (progressDialog.dismiss())

    0 讨论(0)
  • 2021-01-01 00:46

    I use a ProgressBar for this. With a layout like this:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout [...]>
    
      <WebView 
            android:id="@+id/WebView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
      <ProgressBar 
           android:id="@+id/ProgressBar"
           android:layout_centerInParent="true"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           style="?android:attr/progressBarStyleLarge"
           android:visibility="gone"/>
    </RelativeLayout>
    

    I hide and show the progress indicator using:

     WebView webView = (WebView) findViewById(R.id.WebView);
    
     final ProgressBar progess = (ProgressBar) findViewById(R.id.ProgressBar);
    
      webView.setWebViewClient(new WebViewClient() {
      public void onPageStarted(WebView view, String url, Bitmap favicon) {
        progess.setVisibility(View.VISIBLE);
      }
    
      public void onPageFinished(WebView view, String url) {
        progess.setVisibility(View.GONE);
      }
    }
    
    0 讨论(0)
提交回复
热议问题