Deleting Selected Items from ListView in Android

June 21, 2012

In this article, we will create an application in which users select the required items to be deleted and click on “Delete Selected Items” button for deleting them. Actually this article is an extension of the article Dynamically add items to ListView in Android, in which we have created an application to add user input text to listview. In this article, user can add as well as delete items from the listview.

1. Create an Android project namely “ListViewDeleteItems”

New Android Project

Figure 1: New Android Project

2. Select Build Target for this application

Select build target

Figure 2: Select build target

3. Enter application details

Application Details

Figure 3: Application Details

4. Open and update the file res/values/strings.xml with the given below code

<?xml version="1.0" encoding="utf-8"?>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">ListViewDeleteItems</string>
    <string name="hintTxtItem">Enter an item here ...</string>
    <string name="lblBtnAdd">Add Item</string>
    <string name="txtEmpty">List is empty</string>
    <string name="lblBtnDel">Delete Selected Items</string>

5. Open and update the file res/layout/main.xml with the given below code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
    android:orientation="vertical" >




        android:choiceMode="multipleChoice" >

        android:text="@string/lblBtnDel" />

6. Open and update the file /src/in/wptrafficanalyzer/listviewdeleteitems/ with the given below code

package in.wptrafficanalyzer.listviewdeleteitems;

import java.util.ArrayList;

import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ListActivity {

    /** Items entered by the user is stored in this ArrayList variable */
    ArrayList list = new ArrayList();

    /** Declaring an ArrayAdapter to set items to ListView */
    ArrayAdapter adapter;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        /** Setting a custom layout for the list activity */

        /** Reference to the add button of the layout main.xml */
        Button btn = (Button) findViewById(;

        /** Reference to the delete button of the layout main.xml */
        Button btnDel = (Button) findViewById(;

        /** Defining the ArrayAdapter to set items to ListView */
        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, list);

        /** Defining a click event listener for the button "Add" */
        OnClickListener listener = new OnClickListener() {
            public void onClick(View v) {
                EditText edit = (EditText) findViewById(;

        /** Defining a click event listener for the button "Delete" */
        OnClickListener listenerDel = new OnClickListener() {
            public void onClick(View v) {
                /** Getting the checked items from the listview */
                SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
                int itemCount = getListView().getCount();

                for(int i=itemCount-1; i >= 0; i--){

        /** Setting the event listener for the add button */

        /** Setting the event listener for the delete button */

        /** Setting the adapter to the ListView */

7. Execute the application


Application in execution

Figure 4: Application in execution

8. Download source code

9. Reference

How to hire me?

I am George Mathew, working as software architect and Android app developer at

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to

My other blogs

Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :

Tags: , , ,

23 Responses to Deleting Selected Items from ListView in Android

  1. Shikha on September 11, 2012 at 7:44 pm

    Thanks a Lot!!!!!..I was looking for some simple method to delete listview item and I found yours…Its great and easy to implement..Thanks again!!

  2. vsk on September 24, 2012 at 12:02 pm

    its easy and works fine. but how it add check box to listview row. pls explain.

    • george on September 24, 2012 at 12:24 pm

      See the line android:choiceMode=”multipleChoice” in the layout file res/layout/main.xml

  3. mharby on October 4, 2012 at 1:10 am

    Finally, found a simple tutorial for a simple question. :)

  4. sudha on October 22, 2012 at 11:50 am

    i want delete particular item in listview. and i got list view form database. how can i do this?
    please help me…

  5. Steve on November 14, 2012 at 1:58 am

    Nice work.

  6. JB on November 29, 2012 at 2:20 pm

    Thank you for your work!

    Works perfect!!

  7. Sam on December 31, 2012 at 6:43 pm

    Hey the checks on the views remain despite of the removal of views. Didnt any one notice that?

  8. Michael on January 10, 2013 at 10:22 am

    Thanks for the tutorial. As Sam noticed, the checkmarks don’t disappear. This line needs to be added


    It goes in the listenerDel function, right at the end above adapter.notifyDataSetChanged();

    • george on January 10, 2013 at 11:41 am

      Thank you Michael.

      The program is updated by adding the line :


  9. Anuj on January 17, 2013 at 3:50 pm

    Thanks… Great Tutorial… Keep Posting… God bless u :)

  10. rajiv on March 6, 2013 at 3:37 pm

    Thanks……Very Simple and effective coding

  11. George on March 18, 2013 at 11:25 pm

    Hello and thanks for these posts!
    I wanted to ask in the above example how can someone add more fields.I mean , to have a list view which in a row will have for example a Name (textview) ,a surname (textview),an edittext (where he can enter some amount),etc.

    I can’t figure how to use the add and delete buttons (in order to add/delete all the above) in such a case.I want the user to be able to add/delete all the information.


    • nikunj on June 3, 2015 at 2:49 pm


  12. ana on April 30, 2013 at 11:44 pm

    hello sir id error?? what can i do? btnadd,btndel or textitem??? plz rply?

  13. ana on May 1, 2013 at 2:49 pm

    sir string.xml error???

        Enter an item here …
        Add Item
        List is empty
        Delete Selected Items

    dntknw error?

  14. Marlene Duncan on December 29, 2013 at 10:12 am

    I just found this article and it was just what I needed. Works great! Need to be able to save these changes. How can I do this?

  15. divya on December 12, 2014 at 3:48 pm

    bt how to add check box in listview

  16. divya on December 12, 2014 at 4:07 pm

    how to set the color when am i clicking item..plz help me

  17. suman pahari on March 5, 2015 at 5:34 pm

    i want to delete child in expandablelistview and so on sqlite…………..howto do it?

  18. Nurin on August 25, 2015 at 9:27 am

    thank you so much.. it’s help me a lot

  19. Thabiso on October 21, 2015 at 12:47 am

    Thanks…you are the best

  20. beltran on March 21, 2016 at 8:47 pm

    Ton auto m’est très utile pour un project, malheureusement il ne fonctionne pas pour moi,
    Tu aurais une idée de la raison ?

    Voila les erreurs que j’ai :
    Avec le code de base:

    java.lang.RuntimeException: Your content must have a ListView whose id attribute is ‘’
    Merci d’avance

    quand j’enlève le setContentView(R.layout.main); d’après un conseil sur le net ça complexifierais inutilement :
    “java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)’ on a null object reference”

Leave a Reply

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

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner