Swipe Dismiss CardView with Coordinator Layout

Swipe Dismiss CardView with Coordinator Layout

Today we are going to learn an awesome inbuilt functionality provided by design library using Coordinator Layout. CoordinatorLayout is a super-powered FrameLayout. Coordinator Layout is intended for two primary use cases:

  1. As a top-level application decor or chrome layout.
  2. As a container for a specific interaction with one or more child views.

So we are going to implement swipe dismiss the CardView with Coordinate Layout. For this we are going to need :

  1. Coordinator Layout
  2. CardView : A FrameLayout with a rounded corner background and shadow.
  3. SwipeDismissBehavior Class : An interaction behavior plugin for child views of CoordinatorLayout to provide support for the ‘swipe-to-dismiss’ gesture.

Learn more articles about Coordinator Layout here.

VIDEO DEMO

1. Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill required details. By default my activity is MainActivity.java.

2. Open app ⇒ build.gradle  and add below dependencies to it.

3. Now open up your activity_main.xml and place the below code to it. These layout contains CardView inside Coordinator Layout.

4. Now to implement SwipeDismissBeahvior we will be needing below code. Here you can set the swipe dismiss direction and after setting SwipeDismissBehavior you have to get the CardView Layout params and set the behavior to Coordinator Layout.

5. Finally open your MainActivity.java and place the below code to it.

6. Finally, all done. Now you can also make application using Swipe Dismiss Behavior.

Thanks. 🙂

Download Source Code – >

About the author

I am an Android developer for whom Android is the one and only love. I work as a freelancer which gets me and Android closer. I always love to share my learnings and love to learn from others knowledge too.

COMMENTS (1)
Reply

Hi there, just tried this out and it works fine. Just wondering if it was possible to add some form of animation to show the card being swiped away? Thanks 🙂

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)