Swipe to Dismiss -Jetpack Compose

Updated 28 February 2022

Save

In this blog, we are going to learn about gestures in Jetpack Compose. Jetpack compose is a modern toolkit for building native UI. Compose provides a variety of APIs to help you detect gestures that are generated from user interaction. Swipe to dismiss is a single composable function to delete the item from the list and many other functionalities to perform an action like archive the product, move to cart and add to wishlist, etc.

Add dependency

Swipe to dismiss composable function direct offered by the material theme in android.

Create UI in Compose

First of all, we make a UI on which we have to apply gestures. I have created a card view that has some admin details like names and messages. this card is a swipeable view that we can swipe to right or left.

Composable Function

This is the single Composble function to use swipe any view and perform an action in Compose.

State-> state in this composable is performing a vital role. we write our logic inside lambda for the confirmStateChange parameter of the rememberDismissState method.

Modifier-> An ordered, immutable collection of modifier elements that decorate or add behavior to Compose UI elements. For example, backgrounds, padding and click event listeners decorate or add behavior to rows, text, or buttons.

DissmissContent-> That is the place used for the content UI to be visible in the dismiss function.

Background-> That’s the surface is used to background a particular item. Generally, you might add color, icon, text, etc on to indicate to item swipe.

Complete Code

In this code, we make an admin card swipeable to right or left. when cards swipe on the right side they are move-in archive, and the cards swipe on the left side are deleted.

DismissDirection used to card move on different sides. according to directions, we change the color of the card.

Preview

In this preview, you have seen the card move to the right side and the background color is green, which also indicates a text message move to archive.
at the same preview on the move item on the left side but the background color changes to red and also indicates a message move to bin/delete.

Conclusion:

In this blog, we have learned about the Gesture in jetpack compose. we have learned how to implementation of the Swipe to Dismiss Ui create and perform the action in Android.

For more information regarding the compose gesture in the android visit the link.

Thanks for reading this blog. You can also check other blogs from here.

Always be ready for learning 🙂

author
. . .

Leave a Comment

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


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home