how to add Swipe Refresh Layout in android studio

0 2,640

Swipe Refresh Layout android pull to refresh

Swipe Refresh Layout android pull to refresh allow a user to refresh content of an app view via a vertical swipe gesture,in this tutorial I will teach you how can add SwipeRefreshLayout in your android app and change color of Swipe Refresh Layout,in this Swipe Refresh Layout example I used random no to figure out that when a user pulls to refresh how it’s getting update view in app.

Main activity

package exceptionbound.com.swipelayoutrefresh;

import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.TextureView;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

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

      final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipelayout);
        final TextView textView = (TextView) findViewById(R.id.raNumber);
        swipeRefreshLayout.setColorSchemeResources(R.color.refresh,R.color.refresh1,R.color.refresh2);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipeRefreshLayout.setRefreshing(true);
                (new Handler()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipeRefreshLayout.setRefreshing(false);

                        int min = 65;
                        int max = 95;
                        Random random = new Random();
                        int i =random.nextInt(max-min+1)+min;
                        textView.setText(String.valueOf(i));



                    }
                },3000);
            }
        });

    }
}

activity_main

SwipeRefreshLayout Widget

The SwipeRefreshLayout add to the single ListView or GridView as the parent and Remember one thing SwipeRefreshLayout only supports to single ListView or GridView child.
below activity_main.xml shows that how you can add SwipeRefreshLayout widget to your existing or new project.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/swipelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="exceptionbound.com.swipelayoutrefresh.MainActivity">

   <ScrollView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/raNumber"
        />
 </RelativeLayout>
   </ScrollView>

</android.support.v4.widget.SwipeRefreshLayout >

Color

In color.xml we have defined three colors.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>

    <color name="refresh">#008000</color>
    <color name="refresh1">#008080</color>
    <color name="refresh2">#800080</color>


</resources>

Comments

comments

Leave A Reply

Your email address will not be published.