Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How can i show progress bar while loading data into WebView?

How can I show the progress bar while loading data into my webview? My code :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_PROGRESS);
    getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); 
    this.setProgressBarVisibility(true);
    setContentView(R.layout.layout_article);
    WebView webview = (WebView) findViewById(R.id.webView);
    final Activity activity = this;
    webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            activity.setProgress(progress * 100);
        }
    });

    webView.loadUrl("http://google.com");
}
like image 563
yital9 Avatar asked Dec 21 '22 01:12

yital9


2 Answers

Before calling loadData() function, just try to show ProgressDialog or put ProgressBar inside layout.

And inside the WebViewClient, just dismiss() the progressDialog or make the ProgressBar invisible.

for example:

// when finish loading page
public void onPageFinished(WebView view, String url) {
       if(mProgress.isShowing()) {
             mProgress.dismiss();
       }
}

FYI, call loadData() or loadURL() only after you are done with web client setting.

check this example: Load WebView with ProgressDialog

like image 110
Paresh Mayani Avatar answered Dec 27 '22 11:12

Paresh Mayani


Please try following code,

ProgressDialog progDailog = ProgressDialog.show( context,"Process ", "Loading Data...",true,true);

new Thread ( new Runnable()
{
     public void run()
     {
      // your data loading code goes here
     }
}).start();

 Handler progressHandler = new Handler() 
 {

     public void handleMessage(Message msg1) 
     {

         progDailog.dismiss();
         }
 }
like image 42
Altaaf Avatar answered Dec 27 '22 10:12

Altaaf