Swipe Dismiss CardView with Coordinator Layout - Androhub

Swipe Dismiss CardView Banner

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. 🙂

 

2 Comments

Michelle
Friday, September 15th, 2017

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 🙂

Karique
Tuesday, January 16th, 2018

Hello there, did you achieve this? i’m trying to do the same thing.

Post comment

Your email address will not be published. Required fields are marked *