Wednesday, July 16, 2014

Set color scheme of the progress animation in SwipeRefreshLayout

The methods setColorSchemeColors (int color1, int color2, int color3, int color4) and setColorSchemeResources(int colorRes1, int colorRes2, int colorRes3, int colorRes4) of SwipeRefreshLayout set the four colors used in the progress animation. The first color will also be the color of the bar that grows in response to a user swipe gesture.


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.graphics.Color;
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);
  
  swipeRefreshLayout.setColorSchemeColors(
   Color.RED, Color.GREEN, Color.BLUE, Color.CYAN);
 }
 
 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);
  }};
}

Use the same layout file in last exercise of SwipeRefreshLayout example.

1 comment:

  1. HI,

    I am getting below exception.

    extending the activity with ActionbarActivity class


    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.SwipeRefreshLayout.setOnRefreshListener(android.support.v4.widget.SwipeRefreshLayout$OnRefreshListener)' on a null object reference

    please help me out.



    ReplyDelete