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!

3

8 Answers 8

34

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

4
  • 8
    for me it worked with adapter.notifyDataSetChanged() after clearChoices() Jan 30, 2014 at 19:29
  • For me worked with listviewObject.clearChoices () after adapterObject.notifyDataSetChanged ()
    – GFPF
    May 27, 2015 at 21:06
  • adapter.notifyDataSetInvalidated() works for ExpandableListView
    – samus
    Feb 16, 2018 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 3, 2019 at 0: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.

1
  • For me worked with listviewObject.clearChoices () after adapterObject.notifyDataSetChanged ()
    – GFPF
    May 27, 2015 at 21:06
4

For me it worked with:

listView.setAdapter(myAdapter);
3
  • 2
    Thank you. After trying so many different way, this finally worked for me.
    – rastik
    Jul 4, 2016 at 9: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, 2018 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 3, 2019 at 0:13
0

In your XML where you declare the ListView use:

<ListView android:id="@+id/my_list" android:listSelector="#00000000" />
2
  • 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, 2013 at 16:38
  • Oh maybe this can help..
    – user2448968
    Jul 19, 2013 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

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

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);

1
  • What type is your myAdapter? I couldn't find this method neither for ListView nor for ArrayAdapter.
    – bdevay
    Nov 3, 2019 at 0: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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.