Google added nice SwipeRefreshLayout to android.support.v4. With this layout you can easli add swipe to refresh behavior in your application. You only need to embed your layout inside android.support.v4.widget.SwipeRefreshLayout view:

<?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_container" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/list_view" android:layout_weight="1"/> </android.support.v4.widget.SwipeRefreshLayout>

Some example colors for progress bar(default is black):

<?xml version="1.0" encoding="utf-8"?> <resources> <color name="progress_color_1">#004B8E</color> <color name="progress_color_3">#00B15E</color> <color name="progress_color_4">#ED1E79</color> <color name="progress_color_5">#FFA500</color> </resources>

Now we need to add refresh listener and change color. If We dont add refresh listener application will crash on refresh.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.activity_main, null);
  swipeRefreshLayout = (SwipeRefreshLayout)view.findViewById(R.id.swipe_container);
  listView           = (ListView)view.findViewById(R.id.list_view);
  // set this activity to implement SwipeRefreshLayout.OnRefreshListener
  swipeRefreshLayout.setOnRefreshListener(this);
  // Change default colors from black to more nicer
  swipeRefreshLayout.setColorSchemeResources(R.color.progress_color_1, R.color.progress_color_3, R.color.progress_color_4, R.color.progress_color_5);
  return view;
}

// this method is running when somebody will start refresh
@Override
public void onRefresh() {

}

Comments