<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Corentin Houdayer on Medium]]></title>
        <description><![CDATA[Stories by Corentin Houdayer on Medium]]></description>
        <link>https://medium.com/@houdayer.corentin?source=rss-2a0e8c15836c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GdWVJUPQ9imRgFdKTYZPxg.jpeg</url>
            <title>Stories by Corentin Houdayer on Medium</title>
            <link>https://medium.com/@houdayer.corentin?source=rss-2a0e8c15836c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 11:44:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@houdayer.corentin/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to use Swipe To Refresh (SwipeRefreshLayout)]]></title>
            <link>https://medium.com/@houdayer.corentin/how-to-use-swipe-to-refresh-swiperefreshlayout-99abd674c907?source=rss-2a0e8c15836c------2</link>
            <guid isPermaLink="false">https://medium.com/p/99abd674c907</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[pull-to-refresh]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[swiperefreshlayout]]></category>
            <category><![CDATA[android-development]]></category>
            <dc:creator><![CDATA[Corentin Houdayer]]></dc:creator>
            <pubDate>Thu, 13 Dec 2018 10:00:11 GMT</pubDate>
            <atom:updated>2019-01-17T17:59:54.808Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to use Pull To Refresh (SwipeRefreshLayout) in Android</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/526/1*_QrLKYINur2wmb0MYKF1Zg.png" /></figure><p>Hi everyone !</p><p>In this article, you will learn how to implement quicly and simply a working Pull To Refresh feature in your app using the SwipeRefreshLayout in Android.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*9ekU2glFOIfbM8rB0-5mJw.gif" /></figure><h3><strong>Step 1 : Import SwipeRefreshLayout to your Android project</strong></h3><p>First of all, your Android Support Library version should be at least version 19.1.0. The best is to get the latest available version, check your build.gradle, you should have it already implemented.</p><p>implementation &#39;com.android.support:support-v4:19.1.0&#39; or higher</p><p>Okay, now you can use our pretty SwipeRefreshLayout, let’s get started !</p><h3>Step 2 : Add it to your View layout</h3><p>Create an activity and open it.</p><p>Open the corresponding layout, now we are going to add the SwipeRefreshLayout, wrap the component you want. Get in mind that you will have to perform the Pull To Refresh action on the wrapped component.</p><p>For instance, I have a list of items. I would like to refresh it when I do the Pull To Refresh action on it :</p><pre><strong>&lt;android.support.v4.widget.SwipeRefreshLayout<br>    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    android:id=&quot;@+id/swiperefresh_items&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;match_parent&quot;&gt;</strong><br><br>    &lt;ListView<br>        android:id=&quot;@android:id/listview_items&quot;<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;match_parent&quot; /&gt;<br><br><strong>&lt;/android.support.v4.widget.SwipeRefreshLayout&gt;</strong></pre><p>Okay, that’s all for the layout, it’s already implemented, now let’s code a little bit !</p><h3>Step 3 : Implement the Pull To Refresh behavior</h3><p>Go to your activity backend.</p><p>At the top of your activity, add a new variable which will be your SwipeRefreshLayout :</p><pre><strong>public</strong> <strong>class</strong> <strong>TimelineActivity</strong> <strong>extends</strong> Activity <strong>{</strong></pre><pre>    <strong>private</strong> SwipeRefreshLayout mSwipeRefreshLayout<strong>;</strong></pre><pre><em>    ...</em></pre><pre>}</pre><p>Simply link mSwipeRefreshLayout to your layout component on activity creation :</p><pre>@Override <strong>protected</strong> <strong>void</strong> <strong>onCreate(</strong>Bundle savedInstanceState<strong>)</strong> <strong>{</strong></pre><pre>    mSwipeRefreshLayout = findViewById<strong>(</strong>R<strong>.</strong>id<strong>.</strong>swiperefresh_items<strong>);</strong></pre><pre><strong>}</strong></pre><p>Done. Now we are going to define the pull to refresh behavior of our mSwipeRefreshLayout using setOnRefreshListener method.</p><p>We are going to respond to the onRefresh event triggered when we will do the Pull To Refresh action. Better to add it to the <strong>onCreate </strong>method.</p><pre>mSwipeRefreshLayout.<strong>setOnRefreshListener</strong>(new SwipeRefreshLayout.OnRefreshListener() {<br>    @Override<br>    public void <strong>onRefresh</strong>() {<br>        // Your code to make your refresh action<br>        // CallYourRefreshingMethod();<br>   }<br>});</pre><p>Call your UI/Data refreshing method from onRefresh .</p><p>Basically we’re done, you now have to write some code and test your SwipeRefreshLayout.</p><p>However, let’s go a bit deeper in the behavior.</p><h3>Step 4 : Let’s customize our Pull To Refresh action</h3><p>Once you’ll do the action, the spinning arrow will not stop by itself, even though your refreshing method is finished. So we are going to mimic a timer to stop this animation.</p><pre>@Override<br>public void onRefresh() {<br>    // Your code to make your refresh action<br>    // CallYourRefreshingMethod();<br>    final Handler handler = new Handler();<br>    handler.postDelayed(new Runnable() {<br>        @Override<br>        public void run() {</pre><pre>           if(mySwipeRefreshLayout.isRefreshing()) {<br>               mySwipeRefreshLayout.setRefreshing(false);<br>           }</pre><pre>        }<br>    }, 1000);<br>}</pre><p>For the purpose of this example, I’ve created the Handler directly in the onRefresh method, but it is better to make it visible for the whole class by putting it at the top of the activity as a private variable. Also, the duration is set to 1000 ms, feel free to put whatever value you want. The higher you set, the longer will be the spinning animation.</p><p>In case you’re getting data from an external API / database, you won’t know the exact duration of the data retrieving process. Just call the setRefreshing wherever you need it.</p><p>Congrats ! You now have a working Pull To Refresh action in your app.</p><p><strong>Feel free to give any feedback or ask questions </strong>😎</p><p>See you soon !</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=99abd674c907" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>