Adding a Progress Dialog in a webview

后端 未结 4 1920
情书的邮戳
情书的邮戳 2020-12-05 20:32

I have been trying to incorporate a progress dialog into my app when pages are loading in the webview so there isn\'t just a blank white screen. I know this question is pos

相关标签:
4条回答
  • 2020-12-05 20:44

    Try this and I hope, this is what you are after

    webview.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                //Also you can show the progress percentage using integer value 'progress'
                if(!prDialog.isShowing()){
                prDialog = ProgressDialog.show(Grades.this, null, "loading, please wait...");
                }
    
    
                if (progress == 100&&prDialog.isShowing()) {
                    prDialog.dismiss();
                }
            }
        });
    
    0 讨论(0)
  • Kotlin web view with progress dialog working for me. full working code

        var webView = web_view_privacy_policy
        webView?.settings?.javaScriptEnabled = true
        webView?.settings?.loadWithOverviewMode = true
        webView?.settings?.useWideViewPort= true
    
        webView.webViewClient = object : WebViewClient(){
            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                showProgress(getString(R.string.please_wait))
            }
    
            override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
                return true
            }
    
            override fun onPageFinished(view: WebView?, url: String?) {
                hideProgress()
            }
        }
    
        webView.loadUrl(ApiEndPoint.ENDPOINT_PRIVACY_POLICY)
    

    web_view_privacy_policy is id of WebView in .xml

    0 讨论(0)
  • 2020-12-05 20:57

    Check this code,

        final ProgressDialog pd = ProgressDialog.show(OnlinePaymentActivity.this, "", "Please wait, your transaction is being processed...", true);
    
                mWebview  = (WebView)findViewById(R.id.webView1);
    
                    mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
    
                    mWebview.getSettings().setLoadWithOverviewMode(true);
                    mWebview.getSettings().setUseWideViewPort(true);
                    mWebview.getSettings().setBuiltInZoomControls(true);
    
    
                    mWebview.setWebViewClient(new WebViewClient() {
                        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                            Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
                        }
    
                      @Override
                      public void onPageStarted(WebView view, String url, Bitmap favicon)
                      {
                          pd.show();
                      }
    
    
                        @Override
                        public void onPageFinished(WebView view, String url) {
                            pd.dismiss();
    
                                String webUrl = mWebview.getUrl();
    
                                }
    
    }
    
                         });
    
                    mWebview .loadUrl("www.google.com");
    
    0 讨论(0)
  • 2020-12-05 20:57

    Webview with progress dialog working for me. full working code

    import android.app.ProgressDialog;
    import android.graphics.Bitmap;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    
    import com.yubrajpoudel.R;
    
    /**
     * Created by yubraj on 12/25/15.
     */
    public class NewsActivity extends AppCompatActivity {
        private WebView webView;
        ProgressDialog prDialog;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.page_news);
            setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
    
            webView = (WebView) findViewById(R.id.wv_news);
            webView.setWebViewClient(new MyWebViewClient());
    
            String url = "http://google.com/";
            webView.getSettings().setJavaScriptEnabled(true);
            webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            webView.loadUrl(url);
    
    
        }
    
        private class MyWebViewClient extends WebViewClient {
    
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
    
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                prDialog = new ProgressDialog(NewsActivity.this);
                prDialog.setMessage("Please wait ...");
                prDialog.show();
            }
    
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if(prDialog!=null){
                    prDialog.dismiss();
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题