Swipe to refresh Android Activity (SwipeRefreshLayout)

In this tutorial, we will create swipe-to-refresh functionality in the Android. For this purpose, SwipeRefreshLayout widget should be used.

The instance of SwipeRefreshLayout adds an OnRefreshListener method and implements the code logic that will load on refresh. The vertical swipe displays a distinctive progress bar when the user swipes. The progress bar call setRefreshing(true) when it shows the progress animation or calls setRefreshing(false) to cancel.

Swipe to refresh Android Activity Example

In the activity_main.xml file, implement SwipeRefreshLayout widget.

activity_main.xml


Create an activity MainActivity.java and add the following code. In this class, we are checking the network connectivity onRefresh() the swipe.

MainActivity.java


Required Permission

Add the uses-permission in AndroidMenifest.xml file. The below-given permission is used to access network connectivity.

Swipe to refresh Android Activity Swipe to refresh Android Activity