android webview client activity indicator

前端 未结 4 1690
粉色の甜心
粉色の甜心 2020-12-31 09:57

I got the code for showing activity indicator in a webview. I checked more than one reference and still I couldn\'t get it working. Can you please help me to debug my code b

相关标签:
4条回答
  • 2020-12-31 10:41

    I cannot post a comment because I don't have enough reputation points, but just a quick comment on the accepted answer: Check for null before checking if the dialog is showing. This will avoid the dreaded NPE.

    if(pd != null && pd.isShowing()) { ... }
    
    0 讨论(0)
  • 2020-12-31 10:43

    Write below code in Activity's onCreate method.

    webView.setWebChromeClient(new ChromeClient());
    progress=ProgressDialog.show(this, "", "Loading...");
    webView.loadUrl(url);
    

    Create ChromeClient class in same activity.

     private class ChromeClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if(newProgress >= 85) {
                progress.dismiss();
            }
            super.onProgressChanged(view, newProgress);
        }
    }
    

    Declare objects accordingly. Get back to me If you still face error. I will provide full source code.

    0 讨论(0)
  • 2020-12-31 10:45
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.Bundle;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    
    public class SandbarinFacebook extends Activity {
        WebView mWebView;
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...",true);
    
            mWebView = (WebView) findViewById(R.id.webkitWebView1);
            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.getSettings().setSupportZoom(true);  
            mWebView.getSettings().setBuiltInZoomControls(true);
            mWebView.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    if(pd!=null && pd.isShowing())
                    {
                        pd.dismiss();
                    }
                }
            });
            mWebView.loadUrl("http://www.yahoo.co.in");
            setTitle("Yahoo!");
        }
    }
    
    0 讨论(0)
  • 2020-12-31 10:49

    Kotlin snipet:

    myProgressBar.show()
    
    myWebView.webViewClient = object : WebViewClient() {
        override fun onPageFinished(view: WebView, url: String) {
            myProgressBar.hide()
        }
    }
    

    Add this extension functions to your extensions file:

    fun View.show() {
        visibility = View.VISIBLE
    }
    
    fun View.hide() {
        visibility = View.GONE
    }
    
    0 讨论(0)
提交回复
热议问题