ListView - highlight multiple selected items


When implementing the possibility to select multiple items from the list, it would be great to actual see which item are selected and which are not.

I have tried with adding this line:

listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {


but this only highlights the latest selected item. Previous selected items loose their highlighted background color.
How to give all selected items a highlighted background?

Thanks in advance.

EDIT: the answer to this question is just a single page further in the book… I need to finish the chapter before placing questions…




After I have implemented the code as described in listings 18.8 and 18.9 I noticed a strange thing:

  • makes sure you have a multiple number of items in your list
  • longpress a singel item to active the action bar
  • select a second item
  • deselect the second item

The deselected item keeps its gray background colour.
I though of changing listing 18.8 to define a drawable for state_checked (instead of state_activated), as this is what we are looking for in the onActionItemClicked(…) method, but the behaviour does not improve.

Do you have an advice on how to get deselecting to work?


Running the solution on an Android 4.2 emulator, deselecting works fine for me.

Which version of Android are you seeing the issue on? Do you see the issue in the solution as well?


My AVD is set to platform 4.2.2, API Level 17.

I can not get the solution to run. When I try to run it I get the following error:

I have not changed a letter within the project
I have checked the manifest and it seems fine
I have removed the /bin and /gen directories to let them regenerate
I have ‘cleaned’ the project

Thanks in advance


Try adding the support library to the solution and running again.


Thanks for your reply again,

I have re-added the android support library to the project, unfortunately i still can not run the solution. The error stays the same as before.
(I write ‘re-added’ as the ‘android-support-v4.jar’ was already located within the libs\ directory)


I think that error means that your manifest is not referring to the correct file. If you just cut-paste the book code, it is missing the package name.


Adding the package name does not make a difference.
I have tried to import other solutions from the book (just nog i tried chap 20) and they give me t he same error when trying to run them.


1- Are you using Eclipse?
2- Are you using the latest version of the solutions? The latest version of ADT broke compatibility library linking in the old solutions.

Something else you can try in Eclipse: right click the project and choose Android Tools->Fix Project Properties.


Thanks for tying to work this out.
Unfortunately, i still cant get it to work.

1- Yes I am using Eclipse
2- Yes I do have the latest version of the solutions (just re-doanloaded them today)
3- Using Android Tools -> Fix Project Properties dis not help (nothing visually actually happens when clicking on this option)


Hmm, getting odd now. Do you also have the latest version of the Android Tools installed? You can check by running the SDK Manager.


I have just updated the Android Tools:
Android SDK Tool rev 22 -> 22.0.5
Android SDK Platform-tools rev 17 -> 18.0.1

I have deleted the project from my workspace and re-imported it. No change.

I am wondering if others are experiencing this as well?


You wont belief it, but i finally got the imported project (solution) to work… pffff!

I did the following after importing the project into my workspace:

  • right click the project in the Package Explorer
  • click on menu Build Path -> Configure Build Path
  • within the Properties for [project name] dialog the option Java Build Path should be selected at the left hand side
  • at the right hand side open the Order and Export tab
  • the info on that tab says “build class path order and export entries”
  • below that I have selected all available entries (I do not know which options can be left turned off)

After doing this, the project will finally run.