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....
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Solution 2:
In case anyone is looking for a Kotlin solution, this simple one worked for me:
private fun setupWebView() {
val webViewClient: WebViewClient = object: WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
view?.loadUrl(request?.url.toString())
return super.shouldOverrideUrlLoading(view, request)
}
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
showProgressDialog()
super.onPageStarted(view, url, favicon)
}
override fun onPageFinished(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(new WebViewClient() {
@Override
public void onPageCommitVisible(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:
WebView webView = (WebView)findViewById(R.id.webid);
WebSettings websettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setGeolocationEnabled(true);
webSettings.setSupportMultipleWindows(true);
webView.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view,String url){
ProgressBar progressbar = (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:
public class HelloWebViewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
ProgressBar progressBar = 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 ?"