Android ListView Pull to Refresh

In this post, I want to describe a new UI pattern called Android Pull to Refresh. When user pull down it triggers a refresh event on the widget (he is touching) that implements this pattern. This pattern can be applied to several scenarios, I will create a custom ListView widget that behaves like Gmail list, so that when you pull down and you are at the top of the list it will be refreshed. As said, this new UI pattern is known as Pull to refresh. There are several implementation of it this is my own version, very simple.
To have this behavior we have to create a custom component so that we can have a full control. So we have to:

  • Create a custom component that extends the standard ListView widget
  • Create a custom adapter (optional)
  • Create an interface (listener)

Android Pull to refresh: Creating custom component

The first thing we have to do is creating  a custom widget that extends the Android ListView widget and implements the pull to refresh pattern. We have to mind that we have to preserve all the behaviors in the standard ListView. The first thing we have to consider is when the refresh event takes place. There are two conditions that must be satisfied:

  • We are at the top of the list view, in other word the first element is visible
  • User has to touch and move down his finger with a path length longer than a specified threshold
android listview pull to refresh

To verify these requirements we have to override the onTouchEvent and in this method we have to verify if those conditions are satisfied.
public boolean onTouchEvent(MotionEvent event) {
//System.out.println(“First [“+this.getFirstVisiblePosition()+”]”);

float y = event.getY();

switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {
case MotionEvent.ACTION_DOWN: {
startY = y;
STATE_REFRESH_ENABLED = getFirstVisiblePosition() == 0; // We are on the first element so we can enable refresh
case MotionEvent.ACTION_UP: {

return super.onTouchEvent(event);
[/java] In particular, when user touches the screen the first time (MotionEvent.ACTION_DOWN) at line 13-15 we have to verify which item is visible using getFirstVisiblePosition(). If the result of this method is 0 then we can enable the refresh status and check if the user moves down his finger (pull down). At the same time we store the y coord on the touch screen. Then we verify if the user moves down his finger catching the MotionEvent.ACTION_MOVE event (line 8-9). While user pull down his finger and the path length is longer than the a pre-configured threshold then we can trigger the refresh event.

Implement the Refresh event

Once all the conditions are satisfied we can trigger the refresh event. To notify to the caller that the user wants to refresh the list we can use a simple interface with only one method, so the if the caller implements this interface and register itself as a listener it will be notified when a refresh should take place.
[java]public interface OnRefreshListListener {
public void onRefresh();


One thing we can improve is when a refresh status is already triggered and we don’t want to trigger it again. To do it we simply use an internal boolean attribute. Another important thing is to adapt the threshold to the real screen size and dpi.

Of course this isn’t a complete library ready-to-use but it can give some hints.

    1. Vinh June 27, 2013
    2. Jayesh Chandrapal August 1, 2013
    3. Emirali May 29, 2014

    Add Your Comment