Skip to content Skip to sidebar Skip to footer

How To Display A Progress Bar When Webview Loads A Url, In Android ?

I am new in android apps development and learning it. I have developed an application which loads a website link using a WebView. And it's working fine. But I am facing problems to

Solution 1:

try this code... you need webChoromeClient to track the webview load progress...

webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
    progressBar.setProgress(progress);
    if (progress == 100) {
        progressBar.setVisibility(View.GONE);

    } else {
        progressBar.setVisibility(View.VISIBLE);

    }
 }
});

Replace your webview client with this code....

privateclassMyWebViewClientextendsWebViewClient {
@OverridepublicbooleanshouldOverrideUrlLoading(WebView view, String url) {
    view.loadUrl(url);
    returntrue;
   }
}

Solution 2:

In case anyone is looking for a Kotlin solution, this simple one worked for me:

privatefunsetupWebView() {

    val webViewClient: WebViewClient = object: WebViewClient() {

        overridefunshouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
            view?.loadUrl(request?.url.toString())
            returnsuper.shouldOverrideUrlLoading(view, request)
        }

        overridefunonPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
            showProgressDialog()
            super.onPageStarted(view, url, favicon)
        }

        overridefunonPageFinished(view: WebView?, url: String?) {
            hideProgressDialog()
            super.onPageFinished(view, url)
        }
    }
    webView.webViewClient = webViewClient

    webView.settings.javaScriptEnabled = true
    webView.settings.defaultTextEncodingName = "utf-8"
}

Solution 3:

Guaranteed Answer :

        webView.setWebViewClient(newWebViewClient() {

        @OverridepublicvoidonPageCommitVisible(WebView view, String url) {
            super.onPageCommitVisible(view, url);
            mProgressBar.setVisibility(View.INVISIBLE);
        }
    });

Solution 4:

Hey guys, I have useful this method, That would surely help you too.

XML

<WebView
        android:id="@+id/webid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />

Java:

WebViewwebView= (WebView)findViewById(R.id.webid);
WebSettingswebsettings= webView.getSettings();

webSettings.setJavaScriptEnabled(true);
webSettings.setGeolocationEnabled(true);
webSettings.setSupportMultipleWindows(true);


webView.setWebViewClient(newWebViewClient(){

    publicvoidonPageFinished(WebView view,String url){
       ProgressBarprogressbar= (ProgressBar) findViewById(R.id.progress_bar);
       progressbar.setVisibility(View.GONE);
       }
});

Solution 5:

I've added following to my code and it worked very well for me.

XML:

   <WebView
    android:id="@+id/updates_webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"  />

   <ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="?android:attr/progressBarStyleLarge"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

Java:

publicclassHelloWebViewClientextendsWebViewClient {
    @OverridepublicvoidonPageFinished(WebView view, String url) {
        // TODO Auto-generated method stubsuper.onPageFinished(view, url);
        ProgressBarprogressBar=  findViewById(R.id.progressBar1);
        progressBar.setVisibility(View.GONE);
    }
}

Post a Comment for "How To Display A Progress Bar When Webview Loads A Url, In Android ?"