Android SharedPreferences Tutorial and Example

Adding Swipeable Cards with Tinder Style to Android

A card stack can turn a flat profile list into a focused matching flow. The active profile remains visible, follows the user’s finger and either leaves the screen for a pass or triggers a like. On Android, this interaction needs more than a horizontal translation: gesture thresholds, recycled views, accessible actions and decision history all need to work together.

This tutorial uses Kotlin, RecyclerView, MaterialCardView and ItemTouchHelper to build a small swipe deck. You will define profile data, connect left and right gestures, add depth to the stack, support undo and save choices. The same pattern also suits property shortlists, travel recommendations and other card-based browsing experiences.

Build the profile model and card layout

Start with a stable identifier for every profile. A name is a poor key because people can share it, while a server-generated ID stays unique when display details change. Keep profile fields separate from swipe state so the interface can update a decision without rewriting the original record.

data class Profile(
    val id: Long,
    val name: String,
    val age: Int,
    val city: String,
    val photoUrl: String
)

enum class Decision { PASS, LIKE }

MaterialCardView supplies rounded corners, elevation and a consistent surface for photographs and text. Place these cards in a RecyclerView, leave room for the next card to peek behind the active card, and avoid nesting the deck in a ScrollView. Add the AndroidX RecyclerView and Material libraries before building the layout. A fixed image ratio prevents layout jumps while remote photographs load, while placeholders keep empty spaces controlled.

Detect swipes without breaking normal taps

ItemTouchHelper adds horizontal swipe mechanics to RecyclerView without requiring a custom touch parser. Configure left and right directions, retain vertical scrolling and use the callback to identify the selected profile. Treat ItemTouchHelper.RIGHT as a like and ItemTouchHelper.LEFT as a pass rather than inferring a decision from the finger’s final angle.

val swipeCallback = object : ItemTouchHelper.SimpleCallback(
    0,
    ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
) {
    override fun onMove(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder
    ): Boolean = false

    override fun onSwiped(
        viewHolder: RecyclerView.ViewHolder,
        direction: Int
    ) {
        val position = viewHolder.bindingAdapterPosition
        if (position == RecyclerView.NO_POSITION) return

        val profile = adapter.profileAt(position)
        adapter.removeAt(position)

        onDecision(
            profile,
            if (direction == ItemTouchHelper.RIGHT) {
                Decision.LIKE
            } else {
                Decision.PASS
            }
        )
    }
}

Attach the callback after setting the adapter and layout manager. The adapter’s profileAt() and removeAt() methods should read the current list and issue a precise removal notification. A small movement should remain a tap that opens the full profile, while a drag beyond the threshold becomes a decision. Check bindingAdapterPosition before reading data because a recycled holder may no longer represent the visible item, and honour a minimum flick velocity without making slow drags too difficult.

Animate the stack and make decisions reversible

The default ItemTouchHelper movement confirms a swipe, but a polished card deck also needs depth. Hook onChildDraw to update the active card’s translation, slight rotation and opacity, then use a layout decoration or item animator to scale and fade the profiles behind it. Small changes make the next item feel ready without hiding the current action.

override fun onChildDraw(
    canvas: Canvas,
    recyclerView: RecyclerView,
    holder: RecyclerView.ViewHolder,
    dX: Float,
    dY: Float,
    actionState: Int,
    isActive: Boolean
) {
    val card = holder.itemView
    val width = card.width.coerceAtLeast(1).toFloat()

    card.rotation = (dX / width) * 6f
    card.alpha = 1f - (kotlin.math.abs(dX) / width * 0.2f)

    super.onChildDraw(
        canvas,
        recyclerView,
        holder,
        dX,
        dY,
        actionState,
        isActive
    )
}

When onSwiped runs, remove the consumed profile from the submitted adapter list. For undo, retain its previous index, display a Snackbar with an Undo action and reinsert the item with the appropriate adapter notification. If the choice reached storage immediately, mark that record as undone to avoid conflicting history. Reset translation and rotation in clearView so recycled cards never reappear at an angle.

Compare card-stack implementation options

A custom FrameLayout gives complete control over rotation, stacked depth and gestures beyond left and right. It also requires careful touch interception, velocity handling, view recycling and accessibility support, so it suits a learning exercise more readily than a busy production flow.

Approach Interaction Visual control Main trade-off
Custom FrameLayout Fully custom Complete More code to maintain
RecyclerView with ItemTouchHelper Left and right Moderate Extra work for depth
Card-stack library Configurable High Dependency and learning cost
ViewPager2 Page navigation Basic Poor fit for decision queues

For a conventional deck, use RecyclerView with ItemTouchHelper and add depth through a layout decoration or item animator. Choose a dedicated card-stack library when the app needs elaborate transitions, many simultaneous gestures or complex undo rules. ViewPager2 works best for paged content, where each screen is a page rather than a decision that should disappear immediately.

Verify gestures and preserve user decisions

Test the interaction on a physical Android device as well as an emulator, because simulated touches can conceal velocity differences. A short tap should open profile details, vertical movement should remain available where the design permits it, and an abandoned drag should spring back to the centre.

Queue the decision when onSwiped runs, then persist it after the exit animation finishes. Useful fields include the profile identifier, direction, event time and an undone flag; a lone Boolean cannot reconstruct a match history. For durable storage, adapt the Android SQLite examples to those records and keep profile details or access tokens out of logs.

  • Exercise slow drags, quick flicks and diagonal movement.
  • Confirm left, right, cancel and tap paths.
  • Recycle many profiles to expose stale views.
  • Check that undo restores the correct position.

Tune the experience for Australian users

Australia has a busy, competitive dating-app market, so profile details should help people qualify matches quickly. Use Australian English, provide local city filters and keep age and distance preferences visible. Include Sydney, Melbourne, Brisbane, Adelaide, Perth and Canberra without making users hunt through an unfamiliar city list.

Date prompts can reflect local custom. An “arvo coffee” or casual pub meet-up suits Australian social habits, while a Sydney Harbour walk or a Melbourne laneway stop gives location ideas that feel grounded. Show distances in kilometres and travel estimates because metropolitan suburbs can be widely separated, and support matches between metropolitan centres and regional towns.

  • Offer city and suburb filters with a clear reset action.
  • Display distance in kilometres alongside travel time.
  • Cache recent profiles for intermittent reception.
  • Keep report, block and consent controls within reach.

Clear reporting and blocking tools are essential in a crowded market where familiar swipe mechanics can create unwanted interactions. Visible like and pass buttons keep the deck usable with assistive technology and one-handed navigation while preserving the fast swipe flow.