Android RecyclerView Swipe To Delete Example Button And Undo

Android RecyclerView Swipe To Delete Example Tutorial is explained here.

We will make a recyclerview such that user swipe to show button, which will delete the row.

Android RecyclerView Swipe to delete example is similar like gmail app.

We will also implement an UNDO feature, which will restore the deleted row item.

Also Read,

First, check the output of the example, then we will create it with android studio.

Download Source Code For Android RecyclerView Swipe To Delete

Click on below link to download android studio source code.

[sociallocker]RecyclerView Swipe To Delete[/sociallocker]

Step 1. Create a new project.

Always try to create new project with empty activity in android studio.

Step 2. Make row item layout

Prepare new layout resource file named rv_item.xml

Copy following source code in it

This layout file rv_item.xml file represents the layout for each row of recyclerview.

Here, we will create simple row item with just one text in it.

TextView is added to show the name of the row item.

Step 3. Update gradle file

Add below lines in your build.module(Module:app) file

Above three lines add the required classes for recyclerview, cardview and material design respectively.

Step 4. Making Model Class

Make a new JAVA class and give it a name Model.java

Add below code in it

Model class is something like a data transporter for recyclerview.

An object of model class is created for each row item of recyclerview then, all these objects are stored in an ArrayList.

Finally, ArayList will populate the recyclerview in Adapter class.

Step 5. Creating Adapter

A java class named Adapter.java will contain below source code. Go ahead and make new java class.

Adapter will populate the RecyclerView with the help of ArrayList of Model objets.

Two methods of adapter are important here.

Understanding removeItem()

This method will remove the row item when user swipes left or right.

Look at below code

When user swipes left or right, this method will be called from MainActivity.java class along with the position as a parameter.

You can see in above code that an object of model class is removed from ArrayList who is present at position given by parameter.

Understanding resoreItem()

This method will restore the deleted item, when user clicks on UNDO.

Look at following code

When user clicks on UNDO, a model object which was deleted and a position will be passed from Main Activity.

Then, model object will be added at the required position which was also passed from Main Activity as a parameter.

Step 6. Updating MainActivity

Update activity_main.xml as below

Update MainActivity.java class as following

Understanding unableSwipe() method

Refer below code

This method will detect the swipe movement(left or right) of the user.

Then method will decide whether user have swipe left or right. Here, we are deleting row item on both swipe, but you can update them as per your requirement.

After removing the row item, a snackbar will be shown at the bottom of the screen.

This snackbar contains the UNDO button, which can be used to restore the deleted row item.

So it was all about creating Swipe to delete in recyclerview.

If you want to use third party library, then you can use below github library.

https://github.com/hudomju/android-swipe-to-dismiss-undo

But, it is better not to use third party library as it increases the apk file size.

Share our articles with other learners to help them in their journey.

Cheers and Happy coding! 🙂

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

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