Android Tinder Swipe View Example

Tutorial using PlaceHolderView

In this example we will develop views and architecture to simulate the Tinder Swipe.

Objectives of This Tutorial

  1. We would be creating the swipe view as used in the Tinder. Swipe right is considered accepted and swipe left is rejected.
  2. As we can see in the above gif that there are a lot of stuffs happening in the scene. Gestures, animations, view integrity model, dynamic data, view management and lots more. All these are implemented in the SwipePlaceHolderView class in the PlaceHolderView library.
  3. We will load the images from urls and set it in the view display. For this purpose we will use a library Glide.
  4. The profile data list will be seeded in the application and this seed json file will be stored in the assets folder.
  5. The seed file will be parsed into Profile object using another library gson.
  6. This structure will also be compatible if we are pulling url json data from a live server.

SwipePlaceHolderView:

This view is a generic implementation of views in stacked form. The detail about this class can be found here

As we have been doing in my other tutorials, we will take step oriented approach to implement this view in a detailed fashion.

Let’s start the construction:

Step 1:

Set up the project in android studio with default activity.

In app’s build.gradle add the dependencies.

Notes:

  1. Add an assets folder in the src/main directory and point to it in gradle assets.srcDirs
  2. CardView is used to display the image in the deck

Add Internet permission in the app’s AndroidManifest.xml

Step 2:

Create src/layout/activity_main.xml

Step 3:

Create src/layout/tinder_card_view.xml

Notes:

  1. Used fixed size width so get a smooth interaction behavior with the card.

Step 4:

Create src/layout/tinder_swipe_in_msg_view.xml

Notes:

  1. This layout will be provided as swipe state accept/reject indicator on the card.
  2. To avoid text wrapping issue. Make the view of same size as the card in the previous step and then provide a message text at position wherever display is required.

Step 5:

Create src/layout/tinder_swipe_out_msg_view.xml

Step 6:

Place profiles.json file in the assets folder created in the above step 1.

Notes:

  1. This strategy is very useful in bundling app with seed files. Seed files contain data build in the app package and can be used to populate database or use to display default data to the user. Placing seed files in the form of json makes is extremely easy to parse into models.

Step 7:

Create Utils.java

Note:

  1. Utils contain methods required to parse seed json file and also populate the model Profile.java

Step 8:

Create model Profile.java

Notes:

  1. @SerializedName annotation belongs to gson class and used to read json file variable and bind it to the model variable.
  2. @Expose is used to make the variable readable to the gson

Step 9:

We will now create the class to bind the card view and its operations on layout.

Create TinderCard.java for the profile views.

Notes:

  1. @layout is used to bind the layout with this class.
  2. @View is used to bind the views in a layout we want to refer to.
  3. @Resolve annotation bind a method to be executed when the view is ready to be used. Any operation we want to perform on view references should be written in a method and annotated with this.
  4. @SwipeOut calls the annotated method when the card has been rejected.
  5. @SwipeIn calls the annotated method when the card has be accepted.
  6. @SwipeCancelState calls the annotated method when the card is put back in the deck/canceled.
  7. @SwipeInState pings the annotated method till the card is moving in accepted state.
  8. @SwipeOutState pings the annotated method till the card is moving in rejected state.
  9. IMPORTANT: If we don’t plan to re add a view then the class should be annotated with @NonReusable so that the references are released and memory is optimises. For the demonstration of this tutorial we are adding a view back in the deck if rejected, so we have not used @NonReusable.

For detailed explanations view PlaceHolderView at GitHub repository

Step 10:

Create MainActivity.java

Notes:

  1. We obtain the instance of the SwipePlaceHolderView.
  2. We then use the getBuilder() method to modify the default view configurations. In this example we are adding 3 cards in the display and keep adding next card when top card is removed.
  3. SwipeDecor class is used to adjust the visual elements of the view. Here paddingTop and relativeScale gives the perception of a card being placed in stack. The message for card stated is added through setSwipeInMsgLayoutId() and setSwipeOutMsgLayoutId().
  4. We load the json data and parse into Profile Object and add it to the SwipePlaceHolderView list using addView() method.
  5. To programmatically do swiping we call doSwipe() method with flag to indicating accepted or rejected swipe.

PlaceHolderView GitHub repository is here

Note: If you want to auto resize the cards and fix issue of card overlapping the like/dislike buttons for mobiles having bottom navigation bar. Then go through below link for the solution:

https://medium.com/@janishar.ali/modifying-android-tinder-swipe-view-example-to-support-auto-resize-94f9c64f641e

The source code for this example is here

Important developments since the 0.2.7 version:

  1. Dynamic view margin, Undo last swipe, Putback swiped view, Lock view
  2. Disable Swipe on Touch
  3. ItemRemovedListener added for SwipePlaceHolderView
  4. SwipeDirectionalView(Swipe Directions: useful for features like super like), Swipe Touch Callback, Animated Undo, Programmatically Expand/Collapse ExpandablePlaceHolderView

All the remaining release version information can be found here: Link

 

Also, Let’s become friends on Twitter, Linkedin, Github, and Facebook.

Learning is a journey, let’s learn together!

Janishar Ali

Janishar Ali

Co-Founder at Mindorks | Learning is a journey, let’s learn together