Dark Mode
Image

Android Fragments

Android Service

Android AlarmManager

Camera Tutorial

Sensor Tutorial

Android Graphics

Android Animation

Android Web Service

Android MCQ

Android Quiz

Swipe to refresh Android Activity

In this tutorial, we will create swipe-to-refresh functionality in the Android. For this purpose, SwipeRefreshLayout widget should be used.

The instance of SwipeRefreshLayout adds an OnRefreshListener method and implements the code logic that will load on refresh. The vertical swipe displays a distinctive progress bar when the user swipes. The progress bar call setRefreshing(true) when it shows the progress animation or calls setRefreshing(false) to cancel.

Swipe to refresh Android Activity Example

In the activity_main.xml file, implement SwipeRefreshLayout widget.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>  
<android.support.v4.widget.SwipeRefreshLayout 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:id="@+id/refreshLayout"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context="example.javatpoint.com.swiperefreshlayout.MainActivity">  
  
    <RelativeLayout  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content">  
  
        <TextView  
            android:id="@+id/textView"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_alignParentTop="true"  
            android:layout_centerHorizontal="true"  
            android:layout_marginTop="229dp"  
            android:text="Hello World!"  
            android:textSize="18dp"  
            app:layout_constraintBottom_toBottomOf="parent"  
            app:layout_constraintLeft_toLeftOf="parent"  
            app:layout_constraintRight_toRightOf="parent"  
            app:layout_constraintTop_toTopOf="parent" />  
    </RelativeLayout>  
</android.support.v4.widget.SwipeRefreshLayout>  

Create an activity MainActivity.java and add the following code. In this class, we are checking the network connectivity onRefresh() the swipe.

MainActivity.java

package example.javatpoint.com.swiperefreshlayout;  
  
import android.content.Context;  
import android.graphics.Color;  
import android.net.ConnectivityManager;  
import android.net.NetworkInfo;  
import android.support.v4.widget.SwipeRefreshLayout;  
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.widget.TextView;  
  
public class MainActivity extends AppCompatActivity {  
    SwipeRefreshLayout swipeRefreshLayout;  
    TextView textView;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        swipeRefreshLayout = findViewById(R.id.refreshLayout);  
        textView = findViewById(R.id.textView);  
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {  
            @Override  
            public void onRefresh() {  
                       swipeRefreshLayout.setRefreshing(false);  
                       //your code on swipe refresh  
                       //we are checking networking connectivity  
                        boolean connection=isNetworkAvailable();  
                        if(connection){  
                            textView.setText("internet connect");  
                            textView.setTextColor(Color.GREEN);  
                        }  
                        else{  
                            textView.setText("not connected");  
                            textView.setTextColor(Color.RED);  
                        }  
  
            }  
        });  
        swipeRefreshLayout.setColorSchemeColors(Color.YELLOW);  
    }  
    public boolean isNetworkAvailable(){  
  
        ConnectivityManager connectivityManager=(ConnectivityManager) this.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);  
        NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();  
        return networkInfo !=null;  
    }  
}  

Required Permission

Add the uses-permission in AndroidMenifest.xml file. The below-given permission is used to access network connectivity.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  

Swipe to refresh Android Activity Swipe to refresh Android Activity

Comment / Reply From