18 November, 2014

Android - ProgressDialog display before Loading Webpage in WebView

"How to implement progressBar or progressDialog while Webview loading Webpage?" I googled many times for this question and get best answer which i want to post here so it will be helpful to others also.

Basically User Interaction is main thing in any application and this question is also related to User Interaction.

You can implement it using setWebChromeClient() of WebView:

class MyWebChromeClient extends WebChromeClient {

    ProgressDialog pDialog;

    public MyWebChromeClient() {
        // TODO Auto-generated constructor stub
        pDialog = new ProgressDialog(YourActivity.this); //Use getActivity(); for Fragment
        pDialog.setMessage("Loading...");
        pDialog.setCancelable(false);
        pDialog.setProgress(0);
        pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

    }

    public void onProgressChanged(WebView view, int progress) 
    {
        if(progress < 100 && !pDialog.isShowing()){
            pDialog.show();
        }
        pDialog.setProgress(progress);
        if(progress == 100) {
            pDialog.dismiss();
        }
    }
}
and you can set on your WebView like

webView.setWebChromeClient(new MyWebChromeClient());

I have posted this answer on stackoverflow also so you can visit and share it.

Any Question? You can ping me any time.

No comments:

Post a Comment