android webview client activity indicator

梦想与她 提交于 2019-11-30 13:57:30

问题


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 below?

The activity indicator is not coming with below code

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
    final BaseActivity MyActivity = ReviewWebActivity.this;
    setContentView(R.layout.review_web);
    getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
            Window.PROGRESS_VISIBILITY_ON);


    ScannedProduct product =  getReviewUrl();
    reviewUrl = product.getReviewLink();

    if (reviewUrl == null) {
        String err = product.getErrorCode();
        if(err.equals("")) err ="No Data Available for this product";
        Toast.makeText(getApplicationContext(),
                "No Data Available for this product", 1).show();
        return;
    }

    webReview = (WebView) findViewById(R.id.webReview);
    webReview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            // Make the bar disappear after URL is loaded, and changes
            // string to Loading...
            MyActivity.setTitle("Loading...");
            MyActivity.setProgress(progress * 1000); // tried with 100 also


        }
    });
    webReview.setWebViewClient(new ReviewWebClient());
    webReview.getSettings().setJavaScriptEnabled(true);

    webReview.loadUrl(reviewUrl);
}

回答1:


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



回答2:


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.




回答3:


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()) { ... }



回答4:


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
}


来源:https://stackoverflow.com/questions/9310784/android-webview-client-activity-indicator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!