2

I made a news Reader App (WebView). I don't know how to add a pull to refresh in my app. I am a student who just got into programming (Learner/n00b) Anyone here that can kindly let me know what to add or which java class or xml code i have to post here so you can help me out.

Moin Imran
  • 41
  • 1
  • 7
  • Possible duplicate of [SwipeRefreshLayout + WebView when scroll position is at top](http://stackoverflow.com/questions/24658428/swiperefreshlayout-webview-when-scroll-position-is-at-top) – EricRobertBrewer Aug 10 '16 at 20:57

3 Answers3

5

You need to wrap your WebView with a SwipeRefreshLayout. To do so go to your xml file and do the following:

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipeToRefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView.../> <!-- your view goes here -->

</android.support.v4.widget.SwipeRefreshLayout>

Now go to the java file where you have the logic to your WebView (Activity or Fragment) and add the following code:

// Swipe to Refresh
    SwipeRefreshLayout swipeLayout = (SwipeRefreshLayout) getView().findViewById(R.id.swipeToRefresh);
    swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            // Insert your code here
            webView.reload(); // refreshes the WebView
        }
    });

This will set the OnRefreshListener and you can add your code on the method. Check out the official documentation as well to see what other methods the SwipeRefreshLayout have.

Gabriel Schneider
  • 605
  • 2
  • 6
  • 12
0

Wrap your WebView with a SwipeRefreshLayout. Add an OnRefreshListener to that SwipeRefreshLayout that calls reload() on your WebView.

jmcdale
  • 4,463
  • 2
  • 16
  • 20
0

I was trying with SwipeRefreshLayout but got confused. This is what helped me: In Android WebViewGold, within Config.java, just turn ENABLE__PULL_REFRESH to true – hope this helps :-)