Android Listview Swipe To Delete Example Tutorial Step By Step

Android Listview Swipe To Delete Tutorial With Example is covered in this post.

You will learn how to make a listview swipe to show delete button.

We will make a listview in which listview row item will be deleted when the user swipes left or right.

At the end of this example, you will learn to make a listview feature of swipe to delete like in gmail app.

This swipe to remove listview item feature simplifies the deleting process for the user.

First checkout the output video, then we will develop the example step by step.

Step 1. Make a new project.

Create a new project in android studio with empty activity.

Step 2. Adding dimens file

Create a new xml file named dimens.xml under res -> values folder

Add following code in it

Step 3. Updating colors.xml

Update colors.xml file with below

Step 4. Create listview row layout

Make a new layout resource file and give it a name list_item.xml

Add below source code in it

This layout file represents the every single row item of the listview.

In this file, both the layouts are covered :

  • A layout which is shown before the swipe action
  • A layout which is shown after user swipes the row (undo and delete button)

Step 5. Add Library to build.gradle(Module: app)

Add below line into build.gradle(Module: app) file

We have used external library in this example.

The classes of this library will simplify our work and will save our time.

Visit library on github here.

Step 6. Adding Images

download this images and add to drawable folder

Now put these images in the drawable folder.

You can learn how to add items to the drawable folder here in step 2. (No need to create xml file in drawable, just copy and paste images in drawable).

Step 7. Making Model

Create a new java class named Model.java and add below code

Step 8. Preparing Custom Adapter

Make a new java class with name CustomAdapter.java

Add following into it

This adapter class contains remove() method. This method will get position as a parameter. It is the same position (a number of row item) which is swiped by the user.

remove() method will delete the row which it has got as a parameter.

Step 9. Updating Main Activity

Update activity_main.xml as below

Update MainActivity.java file as below

Understanding the main logic

Look closely at following snippet from Main Activity

Above code is the heart of this example. When the user swipes left or right, the compiler will execute the above code lines.

Row item of listview is removed when the user swipes left or right. onDismiss() method will be executed when user swipes.

onDismiss() method will call the remove() method from the adapter class.

A position of the listview row item, which is swiped by the user will be sent as a parameter in the remove() method.

You can add your code as per your requirements in this method.

Undo Execution

When user swipes the row, undo option is shown to him. If user changes his mind after swiping the row, he can undo his task by clicking the undo button.

Read below code

When the user clicks on undo text, above code will be executed.

Download Source Code For Android Listview Swipe To Delete

Click on below link to get android studio source code.

So that is all for android listview swipe to delete tutorial with example.

You can ask your queries in comment without any hesitation.

Thank you and keep sharing our tutorials.

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 *