1

I'm trying to add a CAB (Contextual Action Bar) to a ListView in my app and everything is going well except for one thing. When I initially added the CAB the background color didn't change when I long pressed it although it was selected. My sollution to this was to override onItemCheckedStateChanged and set the background color there. My problem is that when I try to set the background color to what it was earlier I can't. The ListView background appears to be faded which means I can't select a color that blends in with the background. How are you guys doing this? Here's my code:

@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
                                              long id, boolean checked) {

    if (checked) {
        //#6DCAEC is a type of Holo Blue that I want.
        listView.getChildAt(position).setBackgroundColor(Color.parseColor("#6DCAEC"));
    } else {
        //#f1f1f1 was the closest I could get to the background put it still seems out of place
        listView.getChildAt(position).setBackgroundColor(Color.parseColor("f1f1f1"));
    }
}
SweSnow
  • 17,504
  • 10
  • 36
  • 49

2 Answers2

0

Turn off Choice mode on ListView:

mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);

Clear choices (this doesn't affects visible items, just clears ListViews internal choice list)

mListView.clearChoices();

Finally, iterate through visible items and alter them:

 for (int i = 0; i < mListView.getChildCount(); i++) {

        View c = mListView.getChildAt(i);

         //--below code is for un-checking, 
         //--you can do something else, 
         //--like altering the background
        if (c instanceof Checkable) {
            ((Checkable) c).setChecked(false);
        }
    }
S.D.
  • 29,290
  • 3
  • 79
  • 130
  • Unless I call mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); it's not possible to activate the CAB. I'm pretty sure you misunderatood my question. I know how to change the background color (as you can see in my original post). What I'm wondering is how I should change back to my original color of the list view item. – SweSnow Dec 10 '12 at 19:48
0

Try with this code #e7e8e9, tried it on my phone and it works well

found here: Holo light color code of Navigation drawer

Community
  • 1
  • 1
Morendo
  • 752
  • 1
  • 4
  • 23