Swipe to Delete in RecyclerView: A Practical Android Guide
RecyclerView remains the workhorse list component for Android apps shipped from Sydney to Perth, and developers often need to let users remove items with a familiar horizontal gesture. Swipe-to-delete gives users a quick, intuitive way to clean up inbox-style lists, shopping carts, todo items, or recently viewed properties without opening another screen. The pattern is so common that Australian commuters browsing the PTV app in Melbourne or checking their Opal balance on a Sydney train expect it to behave consistently across apps.
This guide walks through the practical steps for adding swipe-to-delete to a RecyclerView, from wiring up ItemTouchHelper to polishing the user experience with undo support and animations. The code works on modern Android Studio Iguana and later, and the same patterns apply whether you are building a hobby project or shipping an app to the Google Play Store in Australia.
Why Swipe Gestures Fit Australian Mobile Behaviour
Australians spend more than five hours a day on their smartphones, according to local research, and that habit shapes how people interact with mobile apps. A swipe gesture to remove an item mirrors the natural flick a person makes when dismissing a notification on a bus in Brisbane or while walking along the Yarra River in Melbourne. Compared to tapping a delete icon, swiping takes less time, requires less accuracy, and feels closer to the rhythm of everyday phone use.
For developers targeting the Australian market, this matters because user retention is closely tied to how familiar an interaction feels. When an Android app behaves the way people expect, the learning curve flattens. Swipe-to-delete also reduces the number of taps needed to clear a list, which helps older devices running the long tail of Android versions still used across regional areas of South Australia and Western Australia. Keeping the gesture lightweight is a small but meaningful contribution to accessibility.
Setting Up the RecyclerView and ItemTouchHelper
The cleanest way to add swipe-to-delete is with the ItemTouchHelper class that ships with the RecyclerView support library. Begin by ensuring your build.gradle file pulls in the latest material library, since the helper classes live alongside the swipe icons and ripple effects:
implementation("androidx.recyclerview:recyclerview:1.3.2")
implementation("com.google.android.material:material:1.12.0")
Inside your Activity or Fragment, attach the ItemTouchHelper to your RecyclerView. You will create a SimpleCallback that defines the swipe directions, then build the helper and attach it directly:
val itemTouchHelper = ItemTouchHelper(
SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT)
)
itemTouchHelper.attachToRecyclerView(recyclerView)
At this point the helper is hooked up but does nothing on swipe, because the callback only declares which directions are valid. The logic that performs the deletion lives in the onSwiped method, which is covered next.
Writing the Swipe Callback Logic
Override the onSwiped method of the SimpleCallback and remove the swiped item from your adapter's data source. The classic pattern uses a copy of the list so the user can undo the action, which is especially helpful for financial apps where users may have tapped the wrong row while checking their account on Adelaide public transport:
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val position = viewHolder.bindingAdapterPosition
val item = items[position]
items.removeAt(position)
adapter.notifyItemRemoved(position)
}
If your list is backed by a Room database, kick off the actual database deletion in a background thread. Using Kotlin coroutines with viewModelScope keeps the UI responsive, but for larger sync operations, you might also reach for Using WorkManager for Background Tasks so the deletion continues even if the user closes the app while waiting at a tram stop in Melbourne.
Handling Undo and Data Refresh
A swipe that silently removes data can frustrate users, particularly when the list contains something they spent time finding. Most production apps offer a Snackbar with an Undo action that reinserts the item if the user taps it within a few seconds. This pattern is easy to add through the onSwiped callback:
Snackbar.make(recyclerView, "Item removed", Snackbar.LENGTH_LONG)
.setAction("Undo") {
items.add(position, item)
adapter.notifyItemInserted(position)
}
.show()
If your adapter displays live data from an API or a Room database, refresh the underlying source after the undo window expires. A practical approach is to call adapter.notifyDataSetChanged() once the Snackbar is dismissed without an undo, then perform the permanent delete on a background coroutine. For more sensitive operations, such as those involving user records governed by the Australian Privacy Principles under the Privacy Act 1988, consider a soft-delete flag instead of a hard delete, so you can honour retention rules without losing the row's history.
Polishing the User Experience
Once the swipe works, the small touches turn the feature from functional to delightful. Start by adding a background colour and a delete icon behind the swiped row, which signals the action visually. The androidx.recyclerview.widget.ItemTouchHelper exposes onChildDraw for exactly this purpose, and a canvas-based icon plus a red rectangle works on devices from entry-level Motorola handsets sold at JB Hi-Fi in Perth to flagship Pixel phones in Sydney.
Haptic feedback is another worthwhile addition. A short vibration on swipe completion reassures users that the gesture registered, and Android provides this through the HapticFeedbackConstants class. Finally, restrict swipes to specific item types when you have heterogeneous rows, such as a header followed by deletable entries. Returning false from getMovementFlags for unwanted rows prevents accidental deletions, which is especially important when the list contains protected content like messages or transaction records.
Choosing the Right Approach for Your App
When swipe-to-delete is implemented well, it disappears into the experience. When it is implemented poorly, users notice immediately, often in reviews left on the Google Play Store. The framework you choose to back the gesture shapes the rest of the implementation, so weigh the trade-offs carefully before settling on a path. The table below compares the most common approaches used by Australian Android developers.
| Approach | Best for | Trade-offs |
|---|---|---|
| ItemTouchHelper with SimpleCallback | Standard lists with simple delete | Requires manual icon drawing, no built-in undo |
| Third-party library like SwipeRevealLayout | Rapid prototyping, rich animations | Extra dependency, less flexibility |
| Contextual Action Mode | Multi-select with batch delete | Two-tap interaction, slower for single delete |
| Custom GestureDetector | Unique gestures beyond swipe | More code, harder to maintain |
Signals You Need Undo Support
- The list contains user-generated content that took effort to add
- ACCC-aligned consumer apps where deletion must be reversible for a short window
- The action is destructive and not easily recreated from memory
Pre-launch Verification
- Confirm deletion direction matches the design system
- Provide undo Snackbar with clear, plain-English copy
- Use a background thread for all database writes
- Add haptic feedback so users feel the gesture register