SwipeRefreshLayout is a core Android UI component that enables the common pull-to-refresh user interface pattern. It provides a natural gesture for users to refresh UI content—commonly used with lists such as RecyclerView or ScrollView.
In this article, we will demonstrate how to integrate SwipeRefreshLayout into your Android application with a complete example, including listener setup and refresh logic.
What Is SwipeRefreshLayout?
SwipeRefreshLayout is a layout container provided by the AndroidX support library that wraps a scrollable view (e.g., RecyclerView, ListView) and adds pull-to-refresh behavior. When the user swipes down from the top of the view, a refresh indicator appears and triggers a callback.
This pattern is familiar in many modern Android apps like news feeds, email clients, and social feeds.
When to Use SwipeRefreshLayout
Use SwipeRefreshLayout when:
- You have scrollable content (RecyclerView, ListView, NestedScrollView)
- You want to provide a manual refresh gesture
- You want to trigger data refresh (network call, database sync)
It is not used for automatic or background refresh — only for user-initiated refresh actions.
Add Dependency
Ensure you have the AndroidX swipe refresh library in your module build.gradle:
dependencies {
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
}
Sync your project after adding the dependency.
Basic SwipeRefreshLayout Setup
Wrap your scrollable content inside SwipeRefreshLayout in your layout file:
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
This wraps a RecyclerView with swipe-to-refresh behavior.
Implement Refresh Logic
In your Activity or Fragment:
SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swipeRefresh);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
// Setup RecyclerView as usual
recyclerView.setLayoutManager(new LinearLayoutManager(this));
List<String> data = new ArrayList<>();
RecyclerViewAdapter adapter = new RecyclerViewAdapter(data);
recyclerView.setAdapter(adapter);
// Set swipe listener
swipeRefreshLayout.setOnRefreshListener(() -> {
// Trigger refresh logic
refreshData(adapter);
});
Refresh Data Method
Here is a simple refresh implementation:
private void refreshData(RecyclerViewAdapter adapter) {
// Simulate a network or database update with delay
new Handler(Looper.getMainLooper()).postDelayed(() -> {
data.clear();
data.add("New Item 1");
data.add("New Item 2");
adapter.notifyDataSetChanged();
// Stop refresh animation
swipeRefreshLayout.setRefreshing(false);
}, 1500); // 1.5s for example
}
This simulates a refresh and updates content accordingly.
Customizing Refresh Indicator
You can customize the spinner color:
swipeRefreshLayout.setColorSchemeResources(
R.color.blue,
R.color.green,
R.color.red,
R.color.yellow
);
This improves UI feedback with theme-aligned colors.
Using SwipeRefreshLayout with NestedScrollView
If you use a scrollable view other than RecyclerView:
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
...>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Content here -->
</androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
ScrollView and NestedScrollView work well with SwipeRefreshLayout.
Best Practices
From real Android engineering experience:
- Always call
setRefreshing(false)when the refresh completes - Avoid heavy UI work directly inside the refresh callback
- Use proper threading (ViewModel + LiveData / Coroutine / RxJava) for network or database operations
- Disable swipe refresh when content cannot scroll to the top
These practices ensure responsive UIs and avoid ANR (Application Not Responding) issues.
Common Issues and Fixes
Swipe not triggering?
✔ Ensure your scrollable view supports vertical scroll and starts at top.
Refresh indicator stays spinning?
✔ Always call swipeRefreshLayout.setRefreshing(false) when done.
Icons colors don’t match theme?
✔ Customize using setColorSchemeResources(...).
Conclusion
SwipeRefreshLayout provides an intuitive pull-to-refresh mechanism that enhances user experience on modern Android apps. With straightforward setup and a clean callback interface, it integrates seamlessly with RecyclerView, ListView, and other scrollable containers.


