Pull to Refresh Functionality in Kotlin Android

Today we are going to discuss how to implement Pull to Refresh Functionality in Kotlin. Finally, Google released an official version of the Pull To Refresh library. It is called SwipeRefreshLayout, inside the support library, and we can use it for RecyclerView, ListView, or other types of views. In this tutorial I am going to explain how to implement Pull To Refresh for RecyclerView using SwipeRefreshLayout on Android.

Pull To Refresh in Android

Import Androidx Appcompat and gridlayout library for SwipeRefreshLayout

Now open the build.gradle file under the Gradle Scripts. After that add the following dependencies under the dependencies block and then sync your project.

implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.gridlayout:gridlayout:1.0.0'
Sync Gradle in Android Studio

Now click the Sync and It will take few mins for sync your project.

XML Code: Wrap RecyclerView inside SwipeRefreshLayout

Now, we have to implement Pull to Refresh functionality, we need to add RecyclerView layout inside SwipeRefreshLayout in the XML code as shown below in the following code.




Activity Code for Pull to Refresh Functionality in Kotlin

Now add the following code inside your Main Activity Java. iniRefreshListener() method will set a listener to the SwipeRefreshLayout and pull to refresh operation is performed by the user.

package com.handyopnion

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

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {

    fun iniRefreshListener() {
        val swipeRefreshLayout=findViewById<SwipeRefreshLayout>(R.id.swipe_layout)
        swipeRefreshLayout.setOnRefreshListener(OnRefreshListener { // This method gets called when user pull for refresh,
            // You can make your API call here,
            val handler = Handler()
            handler.postDelayed(Runnable {
                if (swipeRefreshLayout.isRefreshing()) {
            }, 3000)


In the above code, we are calling the function iniRefreshListener() and to load Xml we have to call the setContentView(R.layout.activity_main) inside onCreate() method in class MainActivity. After adding the above function you will see error at Handler() and you have to import the handler by pressing Alt + Enter and handler import successfully. We just added a delay of 3 seconds. When data refresh is done we need to call SwipeRefreshLayout.setRefreshing(false); method explicitly to dismiss the loading view. If you want to change the delay time then you just have to change the 3000 as shown in the above code.

This is how to implement Pull to Refresh Functionality in Kotlin.


Send Data to Server Using Google Volley using Kotlin

Please share this post:
Posts created 41

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: