Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Disable ListView Selection Highlight but Keep OnClick enabled [duplicate]

I want to disable the highlight that appears when the user selects a row (listSelector) from code. I don't want to disable the onClick and enabled settings (I still want to listen to clicks, just want to remove the highlight).

like image 426
Abdalrahman Shatou Avatar asked Jun 10 '13 21:06

Abdalrahman Shatou


3 Answers

Specify android:listSelector="@android:color/transparent" in your ListView XML.

like image 188
Dororo Avatar answered Nov 13 '22 01:11

Dororo


Just create a drawable that has a transparent color in it, something like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_window_focused="false" android:drawable="@android:color/transparent"/>

<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true"  android:state_enabled="false" android:drawable="@drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true"  android:state_pressed="true" android:drawable="@color/transparent" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@color/transparent" />
<item android:state_focused="true"  android:drawable="@drawable/list_focused_holo" />

</selector>

And then set by code or by XML:

listView.setSelector(R.drawable.my_transparent_selector);

The javadoc for this method says:

Set a Drawable that should be used to highlight the currently selected item.

and the XML attribute is:

android:listSelector

You can play with all the states, remember that you also have the focus state.

like image 6
Nicolas Jafelle Avatar answered Nov 13 '22 02:11

Nicolas Jafelle


I have done this way:

By adding two properties of ListView.

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

Your ListView should looks like below:

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:cacheColorHint="@android:color/transparent"
    android:listSelector="@android:color/transparent">

</ListView>

Done

like image 4
Hiren Patel Avatar answered Nov 13 '22 02:11

Hiren Patel