VOOZH about

URL: https://www.javacodegeeks.com/2012/03/android-load-webview-with-progressbar.html

⇱ Android - Load WebView with ProgressBar - Java Code Geeks


Problem: How to load WebView with ProgressBar?

Description:
Previously, i have published an article for Android – WebViewClient example where we have discussed about how to load URL inside the application instead of opening a native browser.

Now, just consider the case where we want to include progress bar to show loading process. At the same time we can either show ProgressDialog or include ProgressBar inside the XML layout.
– If we include ProgressBar then we have to make it INVISIBLE or GONE whenever page loading is finished
– And if we display ProgressDialog then we have to dismiss it whenever page loading is finished.
So in this tutorial, we are going to display ProgressBar. Just check the snap-2 where ProgressBar is invisible as we have made it invisible.

Output:

Note:
Before implementing this solution, make sure you have added INTERNET permission in your AndroidManifest.xml file.

<uses-permission android:name="android.permission.INTERNET">
</uses-permission>

Solution:
WebViewClientDemoActivity.java

package com.paresh.webviewclientdemo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

/*
 * Demo of creating an application to open any URL inside the application and clicking on any link from that URl
should not open Native browser but that URL should open in the same screen.

- Load WebView with progress bar
 */
public class WebViewClientDemoActivity extends Activity {
 /** Called when the activity is first created. */

 WebView web;
 ProgressBar progressBar;

 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 web = (WebView) findViewById(R.id.webview01);
 progressBar = (ProgressBar) findViewById(R.id.progressBar1);

 web.setWebViewClient(new myWebClient());
 web.getSettings().setJavaScriptEnabled(true);
 web.loadUrl("http://www.technotalkative.com");
 }

 public class myWebClient extends WebViewClient
 {
 @Override
 public void onPageStarted(WebView view, String url, Bitmap favicon) {
 // TODO Auto-generated method stub
 super.onPageStarted(view, url, favicon);
 }

 @Override
 public boolean shouldOverrideUrlLoading(WebView view, String url) {
 // TODO Auto-generated method stub

 view.loadUrl(url);
 return true;

 }

 @Override
 public void onPageFinished(WebView view, String url) {
 // TODO Auto-generated method stub
 super.onPageFinished(view, url);

 progressBar.setVisibility(View.GONE);
 }
 }

 // To handle "Back" key press event for WebView to go back to previous screen.
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event)
 {
 if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
 web.goBack();
 return true;
 }
 return super.onKeyDown(keyCode, event);
 }
}

main.xml

<!--?xml version="1.0" encoding="utf-8"?-->
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">

 <textview android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="This is the demo of WebView Client" android:textsize="20sp" android:gravity="center_horizontal">
 </textview>

 <progressbar android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center" android:id="@+id/progressBar1"> 

 <webview android:id="@+id/webview01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1">
 </webview>
</progressbar></linearlayout>

Download Full source code from here: Android – Load WebView with ProgressBar.

Feedback and reviews are always welcome. Please πŸ‘ :)

Reference: Android – Load WebView with ProgressBar from our JCG partner Paresh N. Mayani at the TechnoTalkative blog.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

πŸ‘ Photo of Paresh Mayani
Paresh Mayani
March 1st, 2012Last Updated: October 21st, 2012
12 258 2 minutes read

Paresh Mayani

Paresh Mayani is a Mobile application developer from India, having been involved in Android app development since around 3 years. He writes technical articles at TechnoTalkative. Apart from his job, he manages Google Developer Group (GDG) - Ahmedabad and has been speaker in various events. He is very much active in supporting the Android developer community, from answering questions on StackOverflow to publishing articles with possible sample code. Currently he is holder of around 25000 reputation.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

12 Comments
Oldest
Newest Most Voted
14 years ago

Thanx for sharing….

0
Reply
Pirate Don
12 years ago
Reply to  Paresh Mayani

bro how can i put admob ads,,,,,,

0
Reply
maria
12 years ago
Reply to  Pirate Don

bro! what is admob ads??:P

0
Reply
11 years ago
Reply to  Pirate Don

I can Help you. which type of Admob you want to integrate Banner or Interstitials…? thanks

0
Reply
in
14 years ago

Great example, thank you. But progress is shown on first load. When other pages loaded, loader is not shown. How to fix this problem?

0
Reply
Philippe Laurent
14 years ago

Perhaps a bit of a hint on how to get the progress bar to show after the initial load (in other words, on each click). Many thanks in advance.

0
Reply
13 years ago

Thanks for the feedback.

0
Reply
13 years ago

Hey man can you update this? I’m getting a bunch of errors on your project because the structure has changed in the latest version of eclipse. Wish I had found this project a year ago. Thanks

0
Reply
gezginbilge
12 years ago

Thanks…

0
Reply
paraiqt
10 years ago

The progress bar just stays a second then you have a blank screen until the url loads. Any idea how to solve this? Thanks

0
Reply
Back to top button
Close
wpDiscuz