Android Android Library

Using Swipe Refresh Layout in Android App

Written by Sabith Pkc Mnr

Android SwipeRefreshLayout is one best way to minimize the use of adding a refresh button at the ActionBar. Talking gently about this layout,Ā the SwipeRefreshLayout should be used whenever the user can refresh the contents of a view via a vertical swipe gesture. The activity that instantiates this view should add an OnRefreshListener to be notified whenever the swipe to refresh gesture is completed.

Today we’re going to have a look at how we can use it inside our Android App within few steps.

As always, I have made a video for you to watch and learn the steps than by reading it. This way you can see the exact steps that you required to do. You can also download the source code in case if you’re a symbol of lazy hard worker šŸ™‚

Source code given at the bottom

Let’s get started

I have added this tutorial on two variants, one you can watch the video tutorial and understand it or you can completely read this post. I suggest reading if you want to know each and every point repeatedly, this may take time, you know!

Video TutorialĀ 

If you want a well-explained video tutorial, then you can watch the below video from my official YouTube channel.


Learn through readingĀ 

Reading is a good practice and always appreciated here šŸ˜€


Step 1: Add Swipe Refresh Layout in the main layout

Firstly, we have to add the swipe refresh layout component inside the main layout, like this:


Here I have added a TextView inside the SwipeRefreshLayout to show the changes once we swiped. Also I have added the Id’s for both of them.

Step 2: Adding reference for both of the items inside MainActivity.

Step 3: Include features of SwipeRefreshLayout

Using the .setColorSchemeColors method, we can add a single or more than one color for the loading spinner of the layout.
Using the above .setOnRefreshListener method, we can tell the layout to do something else when the user swipes the layout.

NOTE: By default, the refreshing will not be finishing after a while, so we have to manually stop refreshing the layout using a Handler().

Here is the with all the lines written inside it.

Complete working:Ā Once the app starts, the initial count of the integer variable numberĀ becomes zero and theĀ TextView get replaced by “Total Swipe = 0” text. Once the user swipes the layout, then it will be firstly calling the .setColorSchemeColors method, and this method decides the color of loading will be going to call the. The new item can be added like this,

Secondly, the .setOnRefreshListener method gets called at the end of spinner loading and here in this activity, it increments the number Integer variable with one and also replaces the TextView with “Total Swipe = 1” text and this increases the count each time the user swipes the layout. After three seconds of refreshing it call the Handler and stop the refreshing.

There you go! If you went up with any trouble while adding this codes to your project, then you can ask your question on below comment section. So, we are about to wrap up this tutorial and hope you guys got the idea from this tutorial. You can direct download the complete source code using the below button.

Source Code

Do you find this tutorial helpful? Do you think respecting the time I took to write this post? If so, please hit the below share buttons to share with your Android loving friends and family members and raise your support. Thank you!

About the author

Sabith Pkc Mnr

Internet Personality, YouTuber, Programmer & Web Entrepreneur! Working on the complete Android platform for more than 5 years!

Leave a Comment