19

I use a ListView in my layout like this:

 <ListView android:id="@+id/list"
              android:layout_width="fill_parent"
              android:layout_gravity="center"
              android:layout_height="match_parent"
              android:layout_weight="0.7"
              android:layout_marginTop="5dp"
              android:orientation="vertical"
              android:layout_centerInParent="true"
              android:divider="@color/dark_grey"
              android:drawSelectorOnTop="false"
              android:focusable="true"
              android:layout_marginBottom="55dp"
              android:cacheColorHint="#00000000"
              android:listSelector="@color/light_grey"
              android:dividerHeight="1px" />

The selector works great but how can I disable the selector?

I tried:

listView.clearChoices();
listView.setSelected();
listView.setSelector();
...

and a few more things but nothing works. Any ideas how I can turn my selected item back to normal? Can't be that complicated, right?

Edit: I need a programmatical solution!

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Ron
  • 22,128
  • 31
  • 108
  • 206
  • 2
    May your are suffering from this bug http://stackoverflow.com/questions/9754170/listview-selection-remains-persistent-after-exiting-choice-mode – M-Wajeeh Jul 19 '13 at 16:40
  • Yeah, when calling `requestLayout()` after `clearChoices()` it works. If you post an anwer I'll accept it – Ron Jul 22 '13 at 06:59
  • Does this answer your question? [How do I clear ListView selection?](https://stackoverflow.com/questions/48253761/how-do-i-clear-listview-selection) – The Dreams Wind Sep 19 '22 at 13:50

8 Answers8

34

Call requestLayout() on the ListView after clearChoices(). It will work.

M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
  • 8
    for me it worked with `adapter.notifyDataSetChanged()` after `clearChoices()` – Stelian Morariu Jan 30 '14 at 19:29
  • For me worked with listviewObject.clearChoices () after adapterObject.notifyDataSetChanged () – GFPF May 27 '15 at 21:06
  • `adapter.notifyDataSetInvalidated()` works for **ExpandableListView** – samus Feb 16 '18 at 19:06
  • Let's warm up this topic. I'm trying to use this in the onDestroyActionMode method of the ActionMode.Callback and doesn't work. The only visible change happens with calling the listView.setAdapter(myAdapter); but it repositioning the screen to the top unexpectedly. – bdevay Nov 03 '19 at 00:18
12

As @Muerte mentioned above, doing the following worked for me:

myListView.clearChoices();
myAdapter.notifyDataSetChanged();

That seems like it would be better than redrawing the whole layout.

Suragch
  • 935
  • 3
  • 9
  • 15
  • For me worked with listviewObject.clearChoices () after adapterObject.notifyDataSetChanged () – GFPF May 27 '15 at 21:06
4

For me it worked with:

listView.setAdapter(myAdapter);
  • 2
    Thank you. After trying so many different way, this finally worked for me. – rastik Jul 04 '16 at 09:27
  • This worked for me. All the other suggestions didn't work. I'm not sure why they didn't but perhaps its because I'm calling myAdapter.notifyDataSetChanged() in the onItemClick() handler. – glez Feb 22 '18 at 17:49
  • The drawback of this solution is that the list will be recreated positioning the screen to the top. It might be annoying sometimes. – bdevay Nov 03 '19 at 00:13
0

In your XML where you declare the ListView use:

<ListView android:id="@+id/my_list" android:listSelector="#00000000" />
  • yeah, but i want my selector to work again for the next click on a list item... if I do this then the selector is invisible, right? – Ron Jul 19 '13 at 16:38
  • Oh maybe [this](http://stackoverflow.com/questions/2038040/android-listview-selector-color) can help.. –  Jul 19 '13 at 16:41
0

You can set a selector after click:

<?xml version="1.0" encoding="utf-8"?>

<item android:drawable="@drawable/transparent_white" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/transparent_white" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="@drawable/transparent_white" android:state_focused="true"/>
<item android:drawable="@drawable/transparent_white" android:state_focused="false"/>

and the @drawable/transparent_white is #00ffffff

kvh
  • 2,118
  • 19
  • 29
0

try this

Create your own selector xml file like this

YourSelector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_enabled="false" android:state_focused="true"
        android:drawable="@drawable/item_disabled" />
  <item android:state_pressed="true"
        android:drawable="@drawable/item_pressed" />
  <item android:state_focused="true"
        android:drawable="@drawable/item_focused" />
</selector>

check out this link

here

Community
  • 1
  • 1
AITAALI_ABDERRAHMANE
  • 2,499
  • 1
  • 26
  • 31
0

Nothing of the solutions actually worked for me. My listview is for listing the searched items and has a provision to redo the search. On getting the result I added the following code snippet and it worked :)

myAdapter.setSelectedIndex(-1);

Rino
  • 1,215
  • 1
  • 16
  • 28
  • What type is your myAdapter? I couldn't find this method neither for ListView nor for ArrayAdapter. – bdevay Nov 03 '19 at 00:15
0

Your selector resource seems to be wrong here. You should use a drawable with transparent background, and i think this is what actually colors your rows, but not choiceMode of the ListView. Please check this another answer for more details.

The Dreams Wind
  • 8,416
  • 2
  • 19
  • 49