Android Delete Multiple Selected Items in ListView Tutorial

In this tutorial, you will learn how to delete multiple selected items in your listview using a contextual action bar (CAB). A contextual action bar appears at the top of the screen to present actions the user can perform on the currently selected items. The user can select multiple items, deselect items, and continue to navigate within the listview. We will create a listview to show texts and images and on listview long item click will allow the users to select multiple items and on delete button click will remove the selected items. So lets begin…

Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project MultipleDeleteListView.

Application Name : MultipleDeleteListView

Project Name : MultipleDeleteListView

Package Name : com.androidbegin.multipledeletelistview

Open your MainActivity.java and paste the following code.

MainActivity.java

In this activity, we have created string arrays with sample data and pass it into the ListViewAdapter class. On listview long item click will allow the user to select multiple items.

We have prepared some sample images for this tutorial. Insert your downloaded sample images into your res > drawable-hdpi.

Sample Images

Flag (16.7 KiB, 3131 downloads)

Next, create an XML graphical layout for your MainActivity. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file listview_main.xml and paste the following code.

listview_main.xml

Next, create an XML graphical layout for your contextual action bar (CAB). Go to res > menu > Right Click on menu New > Android XML File

Name your new XML file activity_main.xml and paste the following code.

activity_main.xml

Next, create an array class. Go to File > New > Class and name it WorldPopulation.java. Select your package named com.androidbegin.multipledeletelistview and click Finish.

Open your WorldPopulation.java and paste the following code.

WorldPopulation.java

Next, create a custom listview adapter. Go to File > New > Class and name it ListViewAdapter.java. Select your package named com.androidbegin.multipledeletelistview and click Finish.

Open your ListViewAdapter.java and paste the following code.

ListViewAdapter.java

In this custom listview adapter class, string arrays are passed into the ListViewAdapter and set into the TextViews  and ImageViews followed by the positions.

Next, create an XML graphical layout for your listview item. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file listview_item.xml and paste the following code.

listview_item.xml

Next, change the application name and texts. Open your strings.xml in your res > values folder and paste the following code.

strings.xml

In your AndroidManifest.xml, set the minimum SDK version to 11. Refer to this link for more info regarding minimum SDK requirements. Open your AndroidManifest.xml and paste the following code.

AndroidManifest.xml

Output:

MultipleDeleteListView ScreenShots

Source Code  

MultipleDeleteListView (287.8 KiB, 752 downloads)
  • Khắc Phục

    Thanks, I did it!

  • Duy Nguyễn

    Please share link download this app, Thank!

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi, have you tried entering your email address in the box provided ?

      • Duy Nguyễn

        i have seen, Thank very much

  • sunil sarode

    it deleting last row if i select first row

    • mirek

      it’s nice bug …

  • sujith thycattussery

    How to change imageview (flags) dynamically?

  • Wiem

    Hi i can search and delete at the same time?

  • Leto505

    Hello, thank for this tutorial.
    I have one question, how are you change the color of the selected item?

  • Shans

    how to do this same code by extending fragment instead of activity and by in listviewadapter extending baseadapter instead of ArrayAdapter ?? please help… thanks in advance

    • gaurav

      I also have the same question