Android: disabling highlight on listView click
Asked Answered
N

15

312

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.

Nittygritty answered 25/5, 2010 at 18:20 Comment(0)
A
687

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.

Anallese answered 25/5, 2010 at 21:0 Comment(12)
if transparent still darkens the selection a little, perhaps it's not defined as completely transparent. Try #00000000 instead, this is totally invisible.Schwenk
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.Limpet
@Anallese 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?Allimportant
From Java Code, you can do listView.setSelector(android.R.color.transparent);Everglades
@Anallese -> "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.Arroyo
Other code alternative would be listView.setSelector(new ColorDrawable(0));Irrelevant
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.Iou
This should be the accepted answer. It actually answers the question with code rather than giving references to fix it.Thorr
Couldn't get it to work without android:cacheColorHint="@android:color/transparent" from Mushtaq answerAltdorf
I tried everything in a PreferenceActivity, but none of those worked. Can somebody help me? It is fustrating to get always that orange selector.Marriott
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 workingVesuvianite
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.Endoenzyme
T
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"
/>
Tote answered 3/9, 2012 at 5:44 Comment(0)
S
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://mcmap.net/q/99143/-android-disabling-highlight-on-listview-click

Slit answered 25/5, 2010 at 18:32 Comment(3)
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?Nittygritty
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.Slit
I am sorry but this shouldn't be the answer. Changing the listSelector should be it.Possible
O
64

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.

Oakley answered 2/11, 2010 at 5:6 Comment(3)
Invalid, that's disabled the selection behaviour (OnItemClicked won't fire) for the itemVirginavirginal
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.Beyer
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.Root
E
40

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

<ListView
android:cacheColorHint="@android:color/transparent"
android:listSelector="@android:color/transparent"/> 
Evante answered 16/9, 2011 at 5:39 Comment(3)
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.Vassaux
"#00000000" and "@android:color/transparent" for kind of variety?Photooffset
@Photooffset ya in 2011 I was letting people know it works both ways... Definitely today you wont need #000000Evante
B
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;
  }
Bellew answered 19/1, 2014 at 4:29 Comment(0)
A
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.

Atmosphere answered 9/2, 2012 at 7:26 Comment(2)
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 ?Madiemadigan
This has a side effect causing ListView not to show dividers.Charlottcharlotta
V
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"
Volcanism answered 5/6, 2012 at 13:14 Comment(0)
A
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>
Ative answered 21/4, 2013 at 12:24 Comment(0)
G
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.

Gillett answered 21/3, 2012 at 19:31 Comment(1)
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"Revolutionary
H
5

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

Homebody answered 3/3, 2013 at 10:59 Comment(0)
A
4

As an alternative:

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

or

listView.setSelector(new StateListDrawable());
Aurochs answered 27/9, 2016 at 10:41 Comment(0)
W
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 ;)

Waste answered 8/1, 2018 at 17:15 Comment(2)
This c# not javaAndrogyne
I think most android programmers are still using java because that is what Android Studio supports, but C# can of cause be used. I just want to clarify that this is C# :DAndrogyne
U
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"

Undesirable answered 19/7, 2016 at 15:45 Comment(0)
O
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

Outmost answered 2/11, 2016 at 9:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.