Blog Details

img
Design

How do you swipe to refresh on Android

Spoke Right / 18 Nov, 2023

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.



 
    
 
        
    

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:


File: AndroidManifest.xml:



    
    
 
        
        
            
 
            
        
    

 

0 comments

Warning: PHP Startup: Unable to load dynamic library 'imagick.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so: cannot open shared object file: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0