Swipe to refresh Android Activity (SwipeRefreshLayout)

The SwipeRefreshLayout widget is used to create swipe-to-refresh functionality in Android. An OnRefreshListener method is added by the instance of the SwipeRefreshLayout. This instance also implements the code logic that will load on refresh. When the user swipes, a distinctive progress bar is displayed by the vertical swipe. When the progress bar shows the progress animation, it either calls the setRefreshing(true) or setRefreshing(false) to cancel.

Example: Swipe to refresh Android Activity :

activity_main.xml:

In the activity_main.xml file, we will write the code to implement the SwipeRefreshLayout widget.

<?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=".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="REFRESH to check the Network Availability!"
            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>

MainActivity.java:

In the MainActivity.java file, we will write the code to check the network connectivity onRefresh() the swipe.

package com.example.radioapp;
 
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("Network Available");
                    textView.setTextColor(Color.BLUE);
                }
                else{
                    textView.setText("Network NOT Available");
                    textView.setTextColor(Color.MAGENTA);
                }
 
            }
        });
        swipeRefreshLayout.setColorSchemeColors(Color.CYAN);
    }
    public boolean isNetworkAvailable(){
 
        ConnectivityManager connectivityManager=(ConnectivityManager) this.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
        return networkInfo !=null;
    }
}

AndroidManifest.xml:

In the AndroidManifest.xml file, we will add the uses-permission to access network connectivity.

Required Permission:

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

File: AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.radioapp">
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
 
        <activity
            android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
 
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
 
</manifest>

Output 1:

Output 2:

Content Protection by DMCA.com
Please Share