Open In App

How to Implement Swipe Down to Refresh in Android

Last Updated : 07 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Certain applications show real-time data to the users, such as stock prices, availability of a product on online stores, etc. Showing real-time data requires continuous syncing of the application, and could be possible by implementing a program such as a thread. A Thread could be initiated by the application and update the real-time information implicitly or explicitly. The deal here is continuously updating the data (maybe from the servers) at the cost of additional RAM, Cache, and Battery of the device resulting in low-performance, as a Thread that runs forever occupies some space and requires power. To avoid using such programs, developers explicitly developed a feature for refreshing the application, so that the user can perform it whenever necessary. This brings us to conclude that manual refreshing has advantages such as:

  • RAM Optimization
  • Cache Memory Optimization
  • Battery Life Optimization
  • Avoiding Unnecessary Callbacks.

For example in the following image when the user will swipe down the screen then the string “Swipe to refresh” will be changed to “Refreshed”.

Swipe Down to Refresh in Android

Step by Step Implementation

Step 1: Create a New Project

To create a new project in the Android Studio, please refer to How to Create/Start a New Project in Android Studio?


Step 2: Adding dependencies

Before start writing the code it is essential to add a Swipe Refresh Layout dependency into the build.Gradle.kts of the application to enable swipe layouts. Navigate to Gradle Scripts > build.gradle.kts (Module :app) and add the following dependency under the dependencies{} scope.

dependencies {
...
implementation ("androidx.swiperefreshlayout:swiperefreshlayout:1.1.0")
}


Step 3: Working with activity_main.xml

It is important to start with the Front-End activity_main.xml“. Create a SwipeRefreshLayout to refresh the Layout and add a TextView to display the string on the screen and provide them with certain IDs. 

activity_main.xml:

XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!--Swipe Refresh Layout -->
    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/refreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!--TextView -->
        <TextView
            android:id="@+id/tv1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="32sp"
            android:text="Swipe to refresh"/>
            
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</LinearLayout>

 
Step 4: Working with MainActivity file

Coming to the MainActivity” file, a preview of the same is provided below. In this file connect the swipeRefreshLayout and textView to its XML file by using the findViewById() method. And also call the setOnRefreshListener() to change the text after the user swipe down the screen. The users can also write the required codes as their needs inside this method. 

MainActivity File:

MainActivity.java
package org.geeksforgeeks.demo;

import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize views
        SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.refreshLayout);
        TextView textView = findViewById(R.id.tv1);

        // Trigger on refresh
        swipeRefreshLayout.setOnRefreshListener(() -> {
            
            // Update text after refresh
            textView.setText("Refreshed");

            // Stop refreshing after one refresh cycle
            swipeRefreshLayout.setRefreshing(false);
        });
    }
}
MainActivity.kt
package org.geeksforgeeks.demo

import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Initialize views
        val swipeRefreshLayout = findViewById<SwipeRefreshLayout>(R.id.refreshLayout)
        val textView = findViewById<TextView>(R.id.tv1)

        // trigger on refresh
        swipeRefreshLayout.setOnRefreshListener{
            
            // implement logic for after refresh
            textView.text = "Refreshed"

            // This line is important as it explicitly refreshes only once
            // If "true" it implicitly refreshes forever
            swipeRefreshLayout.isRefreshing = false
        }
    }
}

Output:


Advantages

Of course, it’s not just the users who benefit. Assuming an application, where the information is fetched directly from a cloud repository. For every callback request (towards the cloud), the developer who owns such a repository pays a minimal amount towards the service, may it be Google Cloud Platform (GCP), Amazon Web Services (AWS), or any other thing. 



Next Article

Similar Reads