How to implement Swipe Down to Refresh in Android using Android Studio

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:

  1. RAM Optimization
  2. Cache Memory Optimization
  3. Battery Life Optimization
  4. 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

Approach:

Step 1: Before started writing the code it is essential to add a Swipe Refresh Layout dependency into the build.Gradle of the application to enable swipe layouts. This dependency is:

implementation “androidx.swiperefreshlayout:swiperefreshlayout:1.1.0”

Step 2: 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. 



XML

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    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="wrap_content"
        android:layout_height="wrap_content"
        android:text="Swipe to refresh"
        />
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
    
</RelativeLayout>

chevron_right


Step 3: Coming to the MainActivity.kt” file, a preview of the same is provided below. In this file connect the swipeRefreshLayout and textView to it’s 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.

Kotlin

filter_none

edit
close

play_arrow

link
brightness_4
code

package org.geeksforgeeks.SwipeRefresh
  
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import org.w3c.dom.Text
  
class MainActivity : AppCompatActivity() {
    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)       
          
    // Declaring a layout (changes are to be made to this)
    // Declaring a textview (which is inside the layout)
    val swipeRefreshLayout = findViewById<SwipeRefreshLayout>(R.id.refreshLayout)
    val textView = findViewById<TextView>(R.id.tv1)
  
    // Refresh function for the layout
    swipeRefreshLayout.setOnRefreshListener{
        
      // Your code goes here
      // In this code, we are just changing the text in the 
      // textbox
      textView.text = "Refreshed"
            
      // This line is important as it explicitly refreshes only once
      // If "true" it implicitly refreshes forever
      swipeRefreshLayout.isRefreshing = false
        }
    }
}

chevron_right


Output: Run on Emulator

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.