Android ProgessBar while loading WebView

后端 未结 12 2522
闹比i
闹比i 2020-12-08 01:52

In my application, I have a WebView which loads any URL from the internet. Now, sometimes due to slow networks the page takes a long time to load and the user s

相关标签:
12条回答
  • 2020-12-08 02:46

    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()
    
        web_container.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                view.loadUrl(url)
                progressBar.show()
                return true
            }
    
            override fun onPageCommitVisible(view: WebView?, url: String?) {
                super.onPageCommitVisible(view, url)
                progressBar.dismiss()
            }
        }
    
    0 讨论(0)
  • 2020-12-08 02:47
      @SuppressLint("SetJavaScriptEnabled")
    private void init() {
        webView = (WebView) findViewById(R.id.kamal);
        webView.setBackgroundColor(0);
        webView.getSettings().setJavaScriptEnabled(true);
        progressDialog = new ProgressDialog(WebView_Ofline.this);
        progressDialog.setMessage("Loading Please wait...");
        progressDialog.setCancelable(false);
        progressDialog.show();
        webView.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                try {
                    progressDialog.dismiss();
                } catch (Exception e) {
                    e.printStackTrace();
    
                }
            }
    
        });
    }
    
    0 讨论(0)
  • 2020-12-08 02:48

    I want to show a progressBar while the webView gets loaded and hide the progessBar when the webView gets loaded completely.

    Following snippet will help you.

    main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <WebView android:id="@+id/webview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
    </LinearLayout>
    

    Main.class

    public class Main extends Activity {
        private WebView webview;
        private static final String TAG = "Main";
        private ProgressDialog progressBar;
    
        /** Called when the activity is first created. */@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            requestWindowFeature(Window.FEATURE_NO_TITLE);
    
            setContentView(R.layout.main);
    
            this.webview = (WebView) findViewById(R.id.webview);
    
            WebSettings settings = webview.getSettings();
            settings.setJavaScriptEnabled(true);
            webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    
            final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    
            progressBar = ProgressDialog.show(Main.this, "Showing ProgressDialog", "Loading...");
    
            webview.setWebViewClient(new WebViewClient() {
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    Log.i(TAG, "Processing webview url click...");
                    view.loadUrl(url);
                    return true;
                }
    
                public void onPageFinished(WebView view, String url) {
                    Log.i(TAG, "Finished loading URL: " + url);
                    if (progressBar.isShowing()) {
                        progressBar.dismiss();
                    }
                }
    
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    Log.e(TAG, "Error: " + description);
                    Toast.makeText(Main.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
                    alertDialog.setTitle("Error");
                    alertDialog.setMessage(description);
                    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            return;
                        }
                    });
                    alertDialog.show();
                }
            });
            webview.loadUrl("http://www.google.com");
        }
    }
    
    0 讨论(0)
  • 2020-12-08 02:48

    You can use something like that:

    override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
        super.onPageStarted(view, url, favicon)
        binding.pbLoader.visibility = ProgressBar.VISIBLE
    }
    
    override fun onPageCommitVisible(view: WebView?, url: String?) {
        super.onPageCommitVisible(view, url)
        binding.pbLoader.visibility = ProgressBar.GONE
    }
    
    0 讨论(0)
  • 2020-12-08 02:53
    String url = "https://stackoverflow.com/questions/11241513/android-progessbar-while-loading-webview";
    setProgressBarVisibility(View.VISIBLE);
    
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            setProgressBarVisibility(View.VISIBLE);
        }
    
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            setProgressBarVisibility(View.GONE);
        }
    
        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            setProgressBarVisibility(View.GONE);
        }
    });
    
    webView.loadUrl(url);
    

    Also add method:

    private void setProgressBarVisibility(int visibility) {
        // If a user returns back, a NPE may occur if WebView is still loading a page and then tries to hide a ProgressBar.
        if (progressBar != null) {
            progressBar.setVisibility(visibility);
        }
    }
    
    0 讨论(0)
  • 2020-12-08 02:54

    Paste This in your code and add your URL

       var progressDialog: ProgressDialog? = null
    
    private fun startWebView(url: String) {
    
        val 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 = ProgressDialog(this)
        progressDialog!!.setMessage("Loading...")
        progressDialog!!.show()
    
        webView.setWebViewClient(object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                view.loadUrl(url)
                return true
            }
    
            override fun onPageFinished(view: WebView, url: String) {
                if (progressDialog!!.isShowing()) {
                    progressDialog!!.dismiss()
                }
            }
    
            override fun onReceivedError(view: WebView, errorCode: Int, description: String, failingUrl: String) {
                Toast.makeText(this@MainActivity, "Error:$description", Toast.LENGTH_SHORT).show()
    
            }
        })
        webView.loadUrl(url)
    }
    
    0 讨论(0)
提交回复
热议问题