Categories

Follow Us @web_learners

Monday, April 6, 2020

How to Add Pull to Refresh in Webview App | Webview App Development Series Part 7

Hi, Today I am going to show you how to Add Pull to Refresh in Webview App. In the Last post, I have shown you How to Add Progress Dialog in Android Webview App. Let's start.


First, we will write code in "activity_main.xml" file, we will take a "SwipeRefreshLayout" above our Webview, and we will close it below "Webview".

swiperefreshlayout width and height will match_parent, and you need to give a id, I am giving id is "swipe".


Code:


We have finished coding in "activity_main.xml" file. So, the whole code will be in activity_main.xml file is :

Code:


Now we will move to "MainActivity.xml" file. First we will take a private SwipeRefreshLayout variable, will give the name "swipeRefreshLayout".

Code:


After that, in "onCreate" function we need to retrieve swiperefreshlayout design from activity_main.xml file and store it to swiperefreshlayout variable which we have taken above.

And Also you need to call onRefreshListener and I will give custom color for SwipeRefreshLayout, you can use default color also. See below code:


Code:



We have finished writing code for "Pull to Refresh". So, the whole code will be in MainActivity.java is:


Code:


Now we will run our project.


as you can see my screenshot we have successfully added pull to Refresh function in our Webview app. Thanks.

3 comments:

  1. Erro on import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

    ReplyDelete
  2. But mam It is showing error <androidx.swiperefreshlayout.widget.SwipeRefreshLayout not found

    ReplyDelete
    Replies
    1. You need to add swiperefreshlayout dependency in your gradle file, like implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"

      Delete