简体   繁体   中英

Progressbar while loading webview in xamarin android

I want to show a progressbar while the webview gets loaded and hide the progessbar when the webview gets loaded completely.

Here is my Activity.cs

  progress = new ProgressDialog(this);
        progress.Indeterminate = true;

        progress.SetProgressStyle(Android.App.ProgressDialogStyle.Spinner);
        progress.SetMessage("Loading... Please wait...");
        progress.SetCancelable(false);
        progress.Show();

        _faq = FindViewById<Android.Webkit.WebView>(Resource.Id.wv_FAQ);

     _web.AppWebViewClients(progress);
     _web.ShouldOverrideUrlLoading(_faq, _url);
     _web.OnPageFinished(_faq, _url);

here is my ResourceWebView.cs

 public class ResourceWebView: WebViewClient
{
    Android.App.ProgressDialog progress;

    public void AppWebViewClients(ProgressDialog progressBar)
    {
        this.progress = progressBar;
        progress.Show();
    }

    public bool ShouldOverrideUrlLoading(WebView view, String url)
    {

        view.LoadUrl(url);
        progress.Show();
        return true;
    }

    public void OnPageFinished(WebView view, String url)
    {
        OnPageFinished(view, url);
        progress.Hide();
    }
}

But this code is not work for me..

Try this

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        urlEditText = (EditText) findViewById(R.id.urlField);
        webView = (WebView) findViewById(R.id.webView);
        webView.setWebViewClient(new MyWebViewClient());

        progress = (ProgressBar) findViewById(R.id.progressBar);
        progress.setVisibility(View.GONE);
        Button openUrl = (Button) findViewById(R.id.goButton);
        openUrl.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                String url = urlEditText.getText().toString();
                if (validateUrl(url)) {
                    webView.getSettings().setJavaScriptEnabled(true);
                    webView.loadUrl(url);

                }
            }

            private boolean validateUrl(String url) {
                return true;
            }
        });

    }

    private class MyWebViewClient extends WebViewClient {   
         @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

         @Override
        public void onPageFinished(WebView view, String url) {
             progress.setVisibility(View.GONE);
                WebViewActivity.this.progress.setProgress(100);
            super.onPageFinished(view, url);
        }

         @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
             progress.setVisibility(View.VISIBLE);
            WebViewActivity.this.progress.setProgress(0);
            super.onPageStarted(view, url, favicon);
        }
    }

Reference this for more reference : http://stacktips.com/tutorials/android/progressbar-while-loading-webview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM