Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 294
user510164 Avatar asked Jun 12 '12 00:06

user510164


1 Answers

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.

like image 196
whyleee Avatar answered Sep 29 '22 17:09

whyleee