Android WebView with Progressbar and Swipe Refresh Layout

Hey Technoz, In this post, we are going to build a WebView with progressbar and swipe refresh functionality. These are two most popular features that enhances webview and present in almost all mobile browsers. Often, these things are simple to implement. So, let us start.

Building Android WebView

We will start by adding a new empty activity to an existing project, or create a new project. We name this new activity WebViewActivity. At first, We are going to build a UI of this webview activity. So, lets put the following xml code in the file activity_web_view.xml. The file is automatically created with creation of activity.

In the above code, we have put the ProgressBar at the top. Obviously, we need the WebView element. As we have to refresh the webview after pull down action by user, we need to put the WebView element inside the SwipeRefreshLayout.

Building the back end

Now, we have to add the back end for WebView with progressbar and swipe refresh. Copy and paste the following code in file WebViewActivity.java.

In above code, we have set the WebView to load Google search page using loadUrl() method. Next, we have find SwipeRefreshLayout by id, and we have setOnRefreshListener to listen the swipe refresh action and perform the following code. So, we are writing a code to get the currently set URL in WebView, and reload it. The refresh icon will continue to show until completion of page loading.

Furthermore, we have to show the ProgressBar while the page is loading. So, we have set the WebViewClient which contains two Overridden methods- onPageStarted() and onPgeFinished(). The ProgressBar is set to ‘GONE’ by default. In onPageStarted method, we have made the progressbar visible. While, In onPageFinished() method, we set the refreshing to false and again make the progressbar invisible.

Later, we have set the WebChromeClient to fill the progressbar with current progress. It continuously checks for the new progress and updates the progressbar. When the progress meets 100%, we again make the android progressbar invisible.

Now, In order to let the app access Internet, we need to add following internet permission in AndroidManifest.xml.

Finally, we are done with WebView with progressbar and swipe refresh layout tutorial! Now try running the app. It will produce the following results.

Android WebView with progressbar and swiperefresh layout
WebView Loading with Progressbar

 

Android WebView with progressbar and swiperefresh layout
Webview refreshing with SwipeRefresh Listener

Thanks for reading this post of WebView with progressbar and swiperefresh layout. If you have any problem about implementing above tutorial, let us know in the comments section below. Alternatively, you can download a source code directly from link below.

Download Source Code

 

Please subscribe our newsletter to get such more knowledgeable tutorials. See you in next tutorial. Happy Coding! 🙂

2 thoughts on “Android WebView with Progressbar and Swipe Refresh Layout

Leave a Reply

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