Android WebView Progress Bar

I looked at a question similar to this one here , but since I'm a beginner, can someone explain how to get this to work in WebView, or at least how to set a 10 second time delay so that people know that it is loading?

+61
android progress-bar webview
Mar 29 '10 at 11:23
source share
9 answers

For a horizontal progress bar, you first need to define a progress bar and link it to your XML file as follows: onCreate :

 final TextView txtview = (TextView)findViewById(R.id.tV1); final ProgressBar pbar = (ProgressBar) findViewById(R.id.pB1); 

Then you can use the onProgressChanged method in your WebChromeClient:

 MyView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { if(progress < 100 && pbar.getVisibility() == ProgressBar.GONE){ pbar.setVisibility(ProgressBar.VISIBLE); txtview.setVisibility(View.VISIBLE); } pbar.setProgress(progress); if(progress == 100) { pbar.setVisibility(ProgressBar.GONE); txtview.setVisibility(View.GONE); } } }); 

After that, there is something like this in your layout.

 <TextView android:text="Loading, . . ." android:textAppearance="?android:attr/textAppearanceSmall" android:id="@+id/tV1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#000000"></TextView> <ProgressBar android:id="@+id/pB1" style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:padding="2dip"> </ProgressBar> 

Here is how I did it in my application.

+117
Dec 11 2018-11-12T00:
source share

I just found a really good example of how to do this here: http://developer.android.com/reference/android/webkit/WebView.html . You just need to change setprogress:

 activity.setProgress(progress * 1000); 

to

 activity.setProgress(progress * 100); 
+21
Mar 29 '10 at 12:40
source share

here is the easiest way to add a progress bar to your android web view.

Add a boolean field in your activity / fragment

 private boolean isRedirected; 

This boolean prevents web pages from being redirected due to dead links. Now you can just pass the WebView object and Url web address to this method.

 private void startWebView(WebView webView,String url) { webView.setWebViewClient(new WebViewClient() { ProgressDialog progressDialog; public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); isRedirected = true; return false; } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); isRedirected = false; } public void onLoadResource (WebView view, String url) { if (!isRedirected) { if (progressDialog == null) { progressDialog = new ProgressDialog(SponceredDetailsActivity.this); progressDialog.setMessage("Loading..."); progressDialog.show(); } } } public void onPageFinished(WebView view, String url) { try{ isRedirected=true; if (progressDialog.isShowing()) { progressDialog.dismiss(); progressDialog = null; } }catch(Exception exception){ exception.printStackTrace(); } } }); webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl(url); } 

Here, on boot, it is called onPageStarted . Here I set the boolean field to false. But when the page finishes loading, it comes to the onPageFinished method, and here the Boolean field is set to true. Sometimes, if the URL is dead, it will be redirected and it will reach onLoadResource() before onPageFinished . For this reason, it will not hide the progress bar. To prevent this, I check if (!isRedirected) on onLoadResource()

in onPageFinished() before rejecting the Progress Dialog, you can write your 10 second delay code

What is it. Happy coding :)

+9
Mar 21 '16 at 10:15
source share

True, there is a more complete option, for example, changing the name of the application for the offer you want. Check out this guide which may help:

http://www.firstdroid.com/2010/08/04/adding-progress-bar-on-webview-android-tutorials/

In this tutorial, you have a complete example of using the progress panel in a webview application.

Adrian.

+4
Aug 04 2018-10-10 at
source share

According to Ms. Sajedul Karim's answer, I wrote a similar answer.

 webView = (WebView) view.findViewById(R.id.web); progressBar = (ProgressBar) view.findViewById(R.id.progress); webView.setWebChromeClient(new WebChromeClient()); setProgressBarVisibility(View.VISIBLE); webView.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); setProgressBarVisibility(View.VISIBLE); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); setProgressBarVisibility(View.GONE); } @Override public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(getActivity(), "Cannot load page", Toast.LENGTH_SHORT).show(); setProgressBarVisibility(View.GONE); } }); webView.loadUrl(url); 



 private void setProgressBarVisibility(int visibility) { // If a user returns back, a NPE may occur if WebView is still loading a page and then tries to hide a ProgressBar. if (progressBar != null) { progressBar.setVisibility(visibility); } } 
+2
Oct 25 '16 at 9:31
source share

Hi, there is a solution for this, please take a look.

  @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); bar.setVisibility(View.GONE); text1.setVisibility(View.GONE); } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); bar.setVisibility(View.VISIBLE); text1.setVisibility(View.VISIBLE); } 

For a complete Webview progress bar example, click here.

+1
Dec 13 '18 at 15:17
source share

Here's how I did it with Kotlin to show percent progress.

My fragment of the layout.

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent"/> <ProgressBar android:layout_marginLeft="32dp" android:layout_marginRight="32dp" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:id="@+id/progressBar"/> </FrameLayout> 

My Kotlin fragment in onViewCreated

  progressBar.max = 100; webView.webChromeClient = object : WebChromeClient() { override fun onProgressChanged(view: WebView?, newProgress: Int) { super.onProgressChanged(view, newProgress) progressBar.progress = newProgress; } } webView!!.webViewClient = object : WebViewClient() { override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) { progressBar.visibility = View.VISIBLE progressBar.progress = 0; super.onPageStarted(view, url, favicon) } override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean { view?.loadUrl(url) return true } override fun shouldOverrideUrlLoading( view: WebView?, request: WebResourceRequest?): Boolean { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { view?.loadUrl(request?.url.toString()) } return true } override fun onPageFinished(view: WebView?, url: String?) { super.onPageFinished(view, url) progressBar.visibility = View.GONE } } webView.loadUrl(url) 
0
Jul 16 '18 at 13:46
source share

Wait for the process to complete ...

 while(webview.getProgress()< 100){} progressBar.setVisibility(View.GONE); 
-one
Apr 18 '13 at 6:46
source share
  webview= (WebView) findViewById(R.id.web); webview.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { WebActivity .setTitle("Loading..."); WebActivity .setProgress(progress * 100); if(progress == 100) WebActivity .setTitle(R.string.app_name); } }); webview.setWebViewClient(new WebViewClient()); webview.getSettings().setJavaScriptEnabled(true); webview.loadUrl("http://google.com"); 

For more details see here http://androiddhina.blogspot.in/2015/05/android-load-webview-with-progressbar.html

-one
May 27 '15 at 4:50
source share



All Articles