310

I want to disable the orange highlight that occurs when touching a listView row. So far in my xml I have tried the following:

android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"

More information: I want there to be zero difference when a user touches the screen on this listView object.

Mycoola
  • 1,135
  • 1
  • 8
  • 29
John Moffitt
  • 5,730
  • 7
  • 30
  • 39

15 Answers15

686

Add this to your xml:

android:listSelector="@android:color/transparent"

And for the problem this may work (I'm not sure and I don't know if there are better solutions):

You could apply a ColorStateList to your TextView.

Mr_Milky
  • 155
  • 9
RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
  • 2
    if transparent still darkens the selection a little, perhaps it's not defined as completely transparent. Try #00000000 instead, this is totally invisible. – Ridcully Apr 01 '12 at 19:27
  • Perfect solution for me. I've tried setting a transparent selector XML drawable for the background of the listviews surrounding layout but this lead to the aforementioned orange bar on ICS devices. – Markus Rudel May 08 '12 at 07:50
  • @RoflcoptrException I use a `Fragment` for the list. The XML layout contains a `LinearLayout` and multiple `TextView` items. Where should I disable the row highlight, please? – JJD Sep 14 '12 at 22:49
  • 43
    From Java Code, you can do `listView.setSelector(android.R.color.transparent);` – sulai Oct 11 '12 at 12:00
  • @RoflcoptrException -> "You could apply a ColorStateList to your TextView" - this is a good option, if the list rows are not clickable. As soon as the textview is clickable, the onClick event of the list itself will not fire. – Sebastian Hojas Nov 28 '12 at 13:36
  • Other code alternative would be `listView.setSelector(new ColorDrawable(0));` – Itai Hanski Apr 09 '13 at 09:07
  • Strangely, setting android:listSelector to `@null` did not work. I had to explicitly set to `@android:color/transparent` to get rid of the yellow selected state drawable. – greg7gkb Feb 04 '15 at 21:28
  • This should be the accepted answer. It actually answers the question with code rather than giving references to fix it. – Dylan Vander Berg Aug 06 '15 at 21:25
  • Couldn't get it to work without `android:cacheColorHint="@android:color/transparent"` from Mushtaq answer – Machado Sep 15 '15 at 11:50
  • I tried everything in a PreferenceActivity, but none of those worked. Can somebody help me? It is fustrating to get always that orange selector. – lacas Mar 03 '17 at 08:50
  • i am geeting frustrated because this line of code was working fine since last year now i edit some code than it stop working, i did contro +z also, but now this code is is not working – Aditay Kaushal Sep 12 '17 at 07:25
  • above solution worked 50% but ImageView gets highlighted. For example, If listview has each view with ImageView's as edit button and delete button. Clicking on a specific view, ImageView gets highlighted. – aashish Dec 29 '17 at 10:37
190

RoflcoptrException's answer should do the trick,but for some reason it did not work for me, So I am posting the solution which worked for me, hope it helps someone

<ListView 
android:listSelector="@android:color/transparent" 
android:cacheColorHint="@android:color/transparent"
/>
Mushtaq Jameel
  • 7,053
  • 7
  • 33
  • 52
101

The orange highlight effect is a style on the ListView. This article gives a good overview of how to override the listView style.

Essentially, you have a selector that specifies different style elements based on the current state.

see this for short and quick solution https://stackoverflow.com/a/12242564/185022

Community
  • 1
  • 1
Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
  • Will this method allow me to have a different style for each listView object? Also, is it possible to avoid the issue I brought up in Sebi's answer? – John Moffitt May 25 '10 at 23:39
  • 1
    Yes, you can override any style attributes. You can specifically assign a style to each ListView using the style="@style/MyStyle" attribute as described in the first link. – Cheryl Simon May 26 '10 at 00:50
  • I am sorry but this shouldn't be the answer. Changing the `listSelector` should be it. – Ayman Salah Aug 23 '16 at 21:10
63

From ListView: Disable Focus Highlight:

When you set your ListAdapter use the following code:

ListAdapter adapter = new SimpleCursorAdapter(MyList, Layout, c, 
                new String[] { "Name", "Score" }, to) 
{ 
    public boolean areAllItemsEnabled() 
    { 
        return false; 
    } 
    public boolean isEnabled(int position) 
    { 
        return false; 
    } 
}; 

This will override the BaseAdapter class. It also cancels the white border between cells.

user16217248
  • 3,119
  • 19
  • 19
  • 37
emdog4
  • 1,975
  • 3
  • 20
  • 25
  • 4
    Invalid, that's disabled the selection behaviour (OnItemClicked won't fire) for the item – Sandor Oct 07 '11 at 09:04
  • 6
    This is a good solution because whether a listview item is highlighted is more of a behavioral issue than an aesthetic one. So, you can use this for the listviews that you don't want to have the property of being selectable. A global style doesn't really make sense, since you know you're eventually going to want a listview that *can* be selected, and using styles for behavior to me seems wrong. They should not have semantic meaning, only aesthetic. The property of selectability is a semantic thing. – Thomas Dignan Nov 15 '12 at 14:22
  • Apart from that this is not the answer to this question, overriding `isEnabled` to disable an item is not the way to disable it! Documentation says that `isEnabled` is supposed to determine if an item is a separator. – Bakhshi Jul 25 '17 at 04:50
39

add this also to ur XMl along with listselector..hope it will work

<ListView
android:cacheColorHint="@android:color/transparent"
android:listSelector="@android:color/transparent"/> 
sheetal
  • 3,014
  • 2
  • 31
  • 45
  • 2
    Many Thanks. I want to solve the problem that the whole listView is hightlighted (white color in my case) when scrolling (Only happen before Android 2.3). And It works. – Yeung Aug 09 '13 at 09:46
  • "#00000000" and "@android:color/transparent" for kind of variety? – Androider Feb 09 '16 at 11:08
  • @Androider ya in 2011 I was letting people know it works both ways... Definitely today you wont need #000000 – sheetal Feb 10 '16 at 15:54
31

If you are using ArrayAdapter or BaseAdapter to populate the list items. Override the isEnabled method and return false.

 @Override
  public boolean isEnabled (int position) {
    return false;
  }
Libin
  • 16,967
  • 7
  • 61
  • 83
13

After a few 'google'ing and testing on virtual and real devices, I notice my below code works:

ArrayAdapter<String> myList = new ArrayAdapter<String>(this, R.layout.list_item, strText) {
    public boolean isEnabled(int position) 
    { 
            return false; 
    } 
};

notice that I've omitted the areAllItemsEnabled() portion.

aerobrain
  • 623
  • 6
  • 8
  • this worked for me . however , doesn't it mean that if there is a drawable for the rows that has the disabled state , it will also change their image ? is it possible to disable clicking and yet not change the drawable ? – android developer Dec 27 '12 at 12:48
  • 1
    This has a side effect causing ListView not to show dividers. – Maciej Pigulski Mar 24 '14 at 09:49
11

Nothing helps me but this:

transparent_drawable.xml:

<?xml version="1.0" encoding="utf-8"?>    
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#00000000"/>
</shape>

layout.xml:

android:listSelector="@drawable/transparent_drawable"
sg7
  • 6,108
  • 2
  • 32
  • 40
7

in code

listView.setSelector(getResources().getDrawable(R.drawable.transparent));

and add small transparent image to drawable folder.

Like: transparent.xml

<?xml version="1.0" encoding="utf-8"?>    
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#00000000"/>
</shape>
Zoker
  • 2,020
  • 5
  • 32
  • 53
6

For me android:focusableInTouchMode="true" is the way to go. android:listSelector="@android:color/transparent" is of no use. Note that I am using a custom listview with a number of objects in each row.

erdomester
  • 11,789
  • 32
  • 132
  • 234
  • Yeah, `android:focusableInTouchMode="true"` allows the view respond to touch, and thus (as far as I can test) its background takes effect rather than the listView's listSelector. If its background is the default transparent then nothing is drawn. When we make a view clickable it also becomes `focusableInTouchMode`, and that's why clickable views get their background drawn from a possible selector like `android:background="@drawable/list_selector_highlight"` – arberg Sep 22 '20 at 17:13
5

You only need to add: android:cacheColorHint="@android:color/transparent"

CuberChase
  • 4,458
  • 5
  • 33
  • 52
Prakash
  • 51
  • 1
  • 1
5

As an alternative:

listView.setSelector(android.R.color.transparent);

or

listView.setSelector(new StateListDrawable());
Pang
  • 9,564
  • 146
  • 81
  • 122
Natali
  • 2,934
  • 4
  • 39
  • 53
1

There is fast and easy way to do this: In method:

private void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    //TODO
    ((ListView)sender).SelectedItem = null;
}

Hope it'll help ;)

FeldonDragon
  • 21
  • 1
  • 4
0

If you want to disable the highlight for a single list view item, but keep the cell enabled, set the background color for that cell to disable the highlighting.

For instance, in your cell layout, set android:background="@color/white"

GLee
  • 5,003
  • 5
  • 35
  • 39
0

you can just get the pos that you get from the onItemClick and do:

listView.setItemChecked(pos, false);

that's the best way i know of

Chief Madog
  • 1,738
  • 4
  • 28
  • 55