I am using multiple choice ListView items. The user can select multiple items. So I wanted to highlight the items selected by the user by changing either the text color or background color of the list row. I have gone through stack overflows links but didn’t get the proper solution. When I am trying to do it, it is randomly changing any unselected item background color. Help!!!
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
videocursor.moveToPosition(position);
v.setBackgroundColor(Color.parseColor("#80B7DBE8"));
SaveSelections();
}
Far better than the above suggestions is to use a selector, also known as a state-list drawable. That way, the OS takes care of all of the business of color highlighting, etc.
more explicitly, take the following xml and save it under res/drawable as something like li_background.xml (I’m using images, but feel free to swap out colors as is appropriate to your situation)
and set the background of All ListItems in the ListView to R.drawable.li_background. if your listView is properly configured for multiple selections (
android:choiceMode="multipleChoice"orlistView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);if you prefer) then android will take care of the rest. This stuff isn’t well documented, I had a hard time finding the solution and I see similar questions all the time, but the relevant dos are here and here