Tuesday, July 15, 2014

SwipeRefreshLayout example

android.support.v4.widget.SwipeRefreshLayout can be used to detect user's vertical swipe gesture, to refresh the contents. It is a simple example of SwipeRefreshLayout, to detect user swipe to do something.


Layout, /res/layout/activity_main.xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    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="com.example.androidswiperefresh.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#A0A0A0" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#808080" >

            <TextView
                android:id="@+id/info"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/ic_launcher"/>
            
        </LinearLayout>
        
    </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>


MainActivity.java
package com.example.androidswiperefresh;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
import android.os.Bundle;
import android.os.Handler;

public class MainActivity extends ActionBarActivity {
 
 SwipeRefreshLayout swipeRefreshLayout;
 TextView textInfo;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  textInfo = (TextView)findViewById(R.id.info);
  
  swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipelayout);
  swipeRefreshLayout.setOnRefreshListener(onRefreshListener);
 }
 
 OnRefreshListener onRefreshListener = new OnRefreshListener(){

  @Override
  public void onRefresh() {
   textInfo.setText("WAIT: doing something");
   
   //simulate doing something
   new Handler().postDelayed(new Runnable() {

    @Override
    public void run() {
     swipeRefreshLayout.setRefreshing(false);
     textInfo.setText("DONE");
    }

   }, 2000);
  }};
}


Next: Set color scheme of the progress animation in SwipeRefreshLayout

No comments: