Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java
  • Android
  • Kotlin
  • Flutter
  • Dart
  • Android with Java
  • Android Studio
  • Android Projects
  • Android Interview Questions
Open In App
Next Article:
How to implement View Shaker in Android
Next article icon

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
How to implement View Shaker in Android
author
aashaypawar
Improve
Article Tags :
  • Android
  • Android Projects
  • Java-Android
  • Kotlin Android

Similar Reads

  • How to Implement MultiSelect DropDown in Android?
    In this article, we are going to see how we can make a MultiSelect DropDown in android studio and will select multiple items from a dropdown list. Advantages of MultiSelect DropDown. It is a good replacement for list boxes as it uses less space does the same work as a list box and gives a good look
    4 min read
  • How to Implement TextSwitcher in Android?
    In Android, TextSwitcher is a useful tool for displaying text with animations. It can be seen as a special version of ViewSwitcher, where its children are only TextView elements. TextSwitcher allows us to add smooth transitions to text, making it part of the transition widget family. Whenever you ca
    3 min read
  • How to implement View Shaker in Android
    View Shaker is an animation in which, the UI of screen vibrates for a limited period of time. This can be implemented on the whole layout or some particular widget. It is a very common effect that developers use, especially to show incorrect credentials. View Shaker helps us to animate the widgets.
    3 min read
  • How to Implement TNImage Library in Android?
    Android is an open-source operating system, based on the Linux kernel and used in mobile devices like smartphones, tablets, etc. Further, it was developed for smartwatches and Android TV. Each of them has a specialized interface. Android has been one of the best-selling OS for smartphones. Android O
    3 min read
  • How to Implement SuperBottomBar in Android App?
    In this article, we are going to implement bottom navigation like there in Spotify. We all have come across apps that have a Bottom Navigation Bar. Some popular examples include Instagram, Snapchat, etc. In this article, let’s learn how to implement an easy stylish SuperBottomBar functional Bottom N
    3 min read
  • How to Implement Press Back Again to Exit in Android?
    The 'Back' button has many different uses in many different android apps. While some app developers use it to close their apps, some use it to traverse back to the app's previous activity. Many apps require the user to press the 'Back' button two times within an interval to successfully close the ap
    2 min read
  • How to Implement Polling in Android?
    Many times you may have seen on some apps like youtube, LinkedIn, etc. polling is done and users choose their options whatever they want to choose. Here we are going to implement polling in Android Studio. What we are going to build in this article? In this article, we will ask the user a question a
    4 min read
  • How to Implement OTP View in Android?
    An OTP View or PinView in android is a widget that allows users to enter their PIN, OTP, etc. They are generally used for two-factor authentication or phone number verification by OTP. A sample video is given below to get an idea about what we are going to do in this article. Note: In order to imple
    2 min read
  • How to Implement Stepper Functionality in Android?
    A Stepper widget allows you to organize content in a sequence of steps, and the user can navigate between them. In this article we are going to implement a Stepper-like functionality in an Android app here we take the help of Tablayout to achieve this. A Sample video is attached below to get an idea
    4 min read
  • How to Implement RecyclerView in a Fragment in Android?
    In Android, a fragment is a modular section of a user interface that can be combined with other fragments to create a flexible and responsive application.  A fragment represents a behavior or a portion of the user interface in an Activity, which can be reused in different activities or layouts. It h
    12 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences