SwipeRefreshLayout

Android provides a widget that implements the swipe-to-refresh design pattern, which allows the user to trigger an update with a vertical swipe. SwipeRefreshLayout widget, which detects the vertical swipe, displays a distinctive progress bar, and triggers callback methods in your app.
To support this, add the widget to your layout file as the parent of the relevant view, and implementing the refresh behavior that gets invoked when the user swipes.

To use the swipe to refresh widget ensure you have a dependency to the support library in your application Gradle build file.when the user swipes.

We Recommend to use new UI components i.e. androidx components


dependencies {
    ....
    // check for latest api
    implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'
    ....
}

Create swiperefresh_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipeRefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textSize="10pt"
            android:text="Text 1" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textSize="10pt"
            android:text="Text 2" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/tv3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textSize="10pt"
            android:text="Text 3" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/tv4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textSize="10pt"
            android:text="Text 4" />
    </androidx.appcompat.widget.LinearLayoutCompat>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

Create SwipeRefreshLayoutActivity.java:
package com.theitbulls.uicomponentex;

import android.os.Bundle;
import android.os.Handler;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

public class SwipeRefreshLayoutActivity extends AppCompatActivity {
    private int count = 1;
    private SwipeRefreshLayout swipeRefresh;

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

        swipeRefresh = findViewById(R.id.swipeRefresh);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                updateOnRefresh();
            }
        });
    }

    private void updateOnRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                AppCompatTextView tv1 = findViewById(R.id.tv1);
                AppCompatTextView tv2 = findViewById(R.id.tv2);
                AppCompatTextView tv3 = findViewById(R.id.tv3);
                AppCompatTextView tv4 = findViewById(R.id.tv4);

                tv1.setText("text1 " + count);
                tv2.setText("text2 " + count);
                tv3.setText("text3 " + count);
                tv4.setText("text4 " + count);

                swipeRefresh.setRefreshing(false);
            }
        }, 2000);
        count++;
    }
}