Easy Swipe to Refresh in Android

Swipe to Refresh Android

Recently, Google released a new version of the Support Library, which has an interesting component. It’s called SwipeRefreshLayout. With that, it’s become a child’s play to implement a quick Swipe-to-Refresh control for your apps.

A few things first

  1. Available only with android-support-v13. Which means that only apps target SDK level 13 and above can use this.
  2. It can only contain one scrollable direct child such as a ListView or a ScrollView.
That’s all you need to know. Well.. A few things more, basically some xml and java code.
For this example, we use a ListView with some demo data. Once the list view is scrolled, we do some task, wait for sometime, and update the list view’s adapter. Finally, ask the SwipeRefreshLayout to stop the progress indicator, since we are done with refreshing.
The layout file

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
    tools:context=”${packageName}.${activityClass}” >
        android:layout_height=”match_parent” >
            android:layout_height=”match_parent” >

The UI initialization

private void initializeViews() {
refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);
                // The default colors for the progress bar are not so nice.
ListView listView = (ListView) findViewById(R.id.listView);
adapter = new ArrayAdapter<String>(this,

The Refresh Task

public void onRefresh() {
Log.i(TAG, “Refresh Requested”);
// Fetch data and update listview’s adapter
private void doRefresh() {
RefreshTask task = new RefreshTask();
task.execute((Void) null);

The Completion Work

private void postRefreshComplete() {
// Stop the refresh animation
// Update adapter with new data
adapter.addAll(getDemoData(new Random().nextInt(20)));

And there you go, a simple swipe to refresh usage for your Android apps. You can find the whole source code here

Step Detector and Step Counter Sensors on Android

Android KitKat has added a few more hardware sensors to it’s API list. Step Sensors are one of them, which looks very promising. Although, not a lot of phones yet have these Step Sensors, in the future, this would gradually become a standard I think. Currently, Nexus 5 has them.

Let’s see how we can interact with these sensors. Basically, there are 2 sensors.

  1. Step Counter: This keeps a count of the number of steps that you have taken. The counter is only reset when you re-boot the device, else, for every step you take (or the phone thinks you took, you counts up).
  2. Step Detector: This sensor just detects when you take a step. That’s it. 
The example project shows you how to initialize and setup the SensorManager and respond to events from the Sensors.

// Step Counter
sManager.registerListener(new SensorEventListener() {
public void onSensorChanged(SensorEvent event) {
float steps = event.values[0];
textViewStepCounter.setText((int) steps + “”);
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}, sManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER),

// Step Detector
sManager.registerListener(new SensorEventListener() {
public void onSensorChanged(SensorEvent event) {
// Time is in nanoseconds, convert to millis
timestamp = event.timestamp / 1000000;
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}, sManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR),

No special permissions are required.

Head over to Github to get the full source code.