How to implement Android Pull-to-Refresh?


Before getting into example, we should know what is Pull to refresh layout in android . we can call pull to refresh in android as swipe-to-refresh. when you swipe screen from top to bottom it will do some action based on setOnRefreshListener.

This example demonstrate about how to implement android pull to refresh.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/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/swipeRefresh"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity">
<LinearLayout
   android:layout_width = "wrap_content"
   android:gravity = "center"
   android:layout_height = "wrap_content">
   <TextView
      android:id = "@+id/text"
      android:textSize = "30sp"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:text = "Hello World!"/>
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>

In the above code we have given swipeRefreshLayout as parent layout, when user swipe layout, it can refresh child view.

Step 3 − Add the following code to src/MainActivity.java

package com.example.andy.myapplication;

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 swipeRefresh;
   static int i = 0;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      final TextView textView = findViewById(R.id.text);
      swipeRefresh = findViewById(R.id.swipeRefresh);
      swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
         @Override
         public void onRefresh() {
            i++;
            textView.setText("Tutorialspoint.com "+i);
            swipeRefresh.setRefreshing(false);
         }
      });
   }
}

In the above code we have given onRefreshListener, when you swipe parent layout it will call onRefresh() from RefreshListener, we are updating test with swipe count as shown below -

swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
   @Override
   public void onRefresh() {
      i++;
      textView.setText("Tutorialspoint.com "+i);
      swipeRefresh.setRefreshing(false);
   }
});

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

In the above result shows the initial screen, now swipe from up to down it will update textview as shown below -


Click here to download the project code

raja
Published on 29-Jan-2019 12:29:49
Advertisements