How to Implement Swipe Refresh in Android

If you want to refresh content in a view on any screen in your android app, then use swipe refresh layout widget. Android support library provides swipe refresh layout widget which allows displaying new content on vertical swipe. In this post, I am going to show how to use swipe refresh layout widget.

How Swipe Refresh Layout Works

The view that needs to refresh on vertical swipe gesture should be defined as child of Swipe refresh layout. Swipe refresh layout captures vertical swipe events on the view and notifies refresh listener.

In the activity of your screen, you need to implement refresh listener SwipeRefreshLayout.OnRefreshListener to handle refresh event notifications from swipe refresh layout.

In onRefresh method of the listener, which gets called on vertical swipe, you need to notify swipe refresh layout about refreshing status by calling setRefreshing method so that refreshing visual can be shown to user while refresh data is being fetched and stopped once refresh is complete.

Swipe Refresh Layout Example

I’ll show you how to use swipe refresh layout with coupons app example. In this example, on coupons list screen, we will display latest coupons on refresh events. We will define RecyclerView to display list of coupons on coupon screen as the direct child of swipe refresh layout in layout xml. To know more about how to implement recycler view and to view recycler view related code, read recycler view related post.

You can customize refresh visual to change color by calling setProgressBackgroundColorSchemeResource.

Swipe Refresh Layout Output

android swipe refresh layout example

Swipe Refresh Layout Code

Swipe refresh activity

package com.zoftino.materialdesign;

import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.design.widget.TabLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public class SwipeRefreshActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private SwipeRefreshLayout srfl;
    private TabLayout tabLayout;
    private Context context;

    protected void onCreate(Bundle savedInstanceState) {

        context = this;

        recyclerView = (RecyclerView) findViewById(R.id.latest_coupons);
        RecyclerView.LayoutManager storeContentLayoutManager = new LinearLayoutManager(this);

        RecyclerView.Adapter contentAdapter = new CouponRecyclerViewAdapter(CouponStoreData.couponsData, this);

        srfl = (SwipeRefreshLayout)findViewById(R.id.store_content_refresh);

                new SwipeRefreshLayout.OnRefreshListener() {
                    public void onRefresh() {

        tabLayout = (TabLayout) findViewById(R.id.store_tabs);

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            public void onTabReselected(TabLayout.Tab tab) {


            public void onTabSelected(TabLayout.Tab tab) {
                String tabId = (String) tab.getText();

            public void onTabUnselected(TabLayout.Tab tab) {



    public void showContent(String tabId) {
        RecyclerView.Adapter contentAdapter = new CouponRecyclerViewAdapter(CouponStoreData.getStoreContent(tabId), this);
    public void refreshContent() {
        int tabPosition = tabLayout.getSelectedTabPosition();
        String tabId = (String)tabLayout.getTabAt(tabPosition).getText();

    public void delayResponseToShowSwipeRefreshVisual(final String tabId){
        new CountDownTimer(4000, 4000) {
            public void onTick(long millisUntilFinished) {

            public void onFinish() {
                RecyclerView.Adapter contentAdapter = new CouponRecyclerViewAdapter(CouponStoreData.getStoreContent(tabId), context);

Swipe refresh activity layout