Tuesday, July 12, 2016

Android swipe to refresh example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#c8ccc7"
    >
    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</android.support.v4.widget.SwipeRefreshLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private SwipeRefreshLayout mSwipeRefreshLayout;
    private ListView mListView;

    private Random mRandom = new Random();
    private List<String> mColors;
    private ArrayAdapter<String> mAdapter;


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

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
        mListView = (ListView) findViewById(R.id.list_view);

        // Initializing a new ArrayList
        mColors = new ArrayList<>();

        for(int i=0;i<5;i++){
            int color = getRandomHSVColor();
            mColors.add(String.valueOf(color));
        }

        // Initialize a new ArrayAdapter instance
        mAdapter = new ArrayAdapter<String>(
                mContext,android.R.layout.simple_list_item_1, mColors){
            @Override
            public View getView(int position, View convertView, ViewGroup parent){
                // Get the current item from ListView
                TextView tv = (TextView) super.getView(position,convertView,parent);

                // Set the background and text color
                tv.setBackgroundColor(Integer.parseInt(mAdapter.getItem(position)));
                tv.setTextColor(Color.BLACK);

                // Return the TextView
                return tv;
            }
        };

        // Data bind ArrayAdapter with ListView
        mListView.setAdapter(mAdapter);

        // Set a Refresh Listener for the SwipeRefreshLayout
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // Refresh the data
                // Calls setRefreshing(false) when it is finish
                updateOperation();
            }
        });
    }

    protected void updateOperation(){
        // Add a new color to the list top
        mColors.add(0,String.valueOf(getRandomHSVColor()));

        // Notify the ArrayAdapter about recent changed
        mAdapter.notifyDataSetChanged();

        // Stop progress indicator when update finish
        mSwipeRefreshLayout.setRefreshing(false);
    }

    // Custom method to generate random HSV color
    public int getRandomHSVColor(){
        // Generate a random hue value between 0 to 360
        int hue = mRandom.nextInt(361);
        // We make the color depth full
        float saturation = 1.0f;
        // We make a full bright color
        float value = 1.0f;
        // We avoid color transparency
        int alpha = 255;
        // Finally, generate the color
        int color = Color.HSVToColor(alpha, new float[]{hue, saturation, value});
        // Return the color
        return color;
    }
}