user510164
user510164

Reputation: 646

Android listview entire list getting selected

I seem to be having a UI problem with a listview. I have a listview and selecting any item on the list, highlights the entire listview. The selection is working fine, I get the correct item in the listener.

But the problem is that when i select any item, the entire listview gets highlighted, so it is difficult to tell which row got selected.

This is working fine on Android >3.1 - Currently i am testing on a 2.3 device.

<ListView
        android:id="@+id/myList"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginTop="@dimen/margin_medium"
        android:background="@drawable/border"
        android:listSelector="#99000000"
        android:scrollbarFadeDuration="1000000"
        android:scrollbars="vertical" >
    </ListView>

Upvotes: 12

Views: 1875

Answers (3)

Lisa Wray
Lisa Wray

Reputation: 2272

If you don't want to change your selector, you can also set it as the background of the list item's layout, rather than in the listSelector field of the ListView. The list items receive the "selected" state when touched and the color is displayed properly.

This works for all versions of Android.

Ex: This is your list item layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background_color">
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Your text here" />
</LinearLayout>

And your list with no list selector:

<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

Upvotes: 1

whyleee
whyleee

Reputation: 4049

I recently had the same issue, but the reason was in Android < 3.0 bug: if you set a @color as a drawable for your list item pressed selector, then it will fill the entire list area. Solution is to use a 'shape' drawable instead of @color.

So, create a new XML file in res/drawable folder with similar content:

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

    <solid android:color="@color/my_list_item_pressed_color" />
</shape>

And reference created drawable in your ListView selector for state_pressed (also created as an XML file in res/drawable folder):

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/list_item_pressed_bg" />
    <item android:drawable="@android:color/transparent" />
</selector>

And use this selector in your ListView:

<ListView
    .....
    android:listSelector="@drawable/list_item_selector" />

That's all. Works at least with Android 2.2-2.3.

Upvotes: 26

user510164
user510164

Reputation: 646

I fixed this by removing this line: (not sure why it worked)

android:listSelector="#99000000"

Upvotes: -3

Related Questions