4

In my app I retrieve a user's playlist from the server and feel a ListView with the data. The problem is, that the ListView does not highlight clicked rows. To prevent rage comments telling me that I don't use search here are things I've tried:

  • setting the button in my row to non-focusable
  • setting the listSelector both by code and XML (not at the same time)
  • trying to change the background of passed View in an OnItemClickListener
  • using setItemsCanFocus() with bot true and false parameters
  • setting an XML drawable with states "selected" and "pressed" to row layout root

Clicks are detected since I tested that using a Toast. But I just can't force the ListView to highlight the selected row

ListView

        <ListView
    android:id="@android:id/list"
    android:listSelector="@drawable/list_selected"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/etbg"
    android:choiceMode="singleChoice"
    android:divider="@color/div" >
</ListView>

Layout used as list row

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
 >

<TextView
    android:id="@+id/tvListItemArtist"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="14dp"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="Small Text"
    android:layout_toRightOf="@+id/tbListPlay"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#FFFFFF" />

<TextView
    android:id="@+id/tvListItemSong"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/tvListItemArtist"
    android:layout_centerVertical="true"
    android:layout_marginLeft="5dp"
    android:layout_toRightOf="@+id/tvListItemArtist"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="Small Text"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#FFFFFF" />

<ImageView
    android:id="@+id/img_list_audio_saved"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="50dp"
    android:src="@drawable/attention"
    android:visibility="gone" />

<Button
    android:id="@+id/tbListPlay"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/button_play"
    android:focusable="false"
    android:focusableInTouchMode="false" />

     </RelativeLayout>

Any suggestions?

Droidman
  • 11,485
  • 17
  • 93
  • 141

3 Answers3

2

you have changed your ListSelector: android:listSelector="@drawable/list_selected" I assume your custom drawable is not a selector, which has different states. See this site or this question to understand the usage

Community
  • 1
  • 1
Nickolaus
  • 4,785
  • 4
  • 38
  • 60
  • You can find the selectors in your android folder: android-sdk\platforms\android-14\data\res\drawable. Then copy it into your project and change it to your needs. – Warpzit Mar 07 '13 at 18:07
  • well, I tried to remove the listSelector attribute which means the default one should be used. No effect – Droidman Mar 07 '13 at 18:09
1

Create a selector for the list view in your drawables folder and set the list item background to this drawable. See the code below

select_button_background.xml

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/select_button_selected" android:state_enabled="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/select_button_selected" android:state_enabled="true" android:state_selected="true"/>

    <item android:drawable="@drawable/select_button_selectable" android:state_pressed="false"/>
    <item android:drawable="@drawable/select_button_selectable" android:state_selected="false"/>
 </selector>

select_button_selectable.xml

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

    <gradient
        android:angle="270"
        android:endColor="#CCCCCC"
        android:startColor="#DDDDDD"
        android:type="linear" />

    <corners android:radius="10dp" />

    <shape>
        <stroke
            android:width="1dp"
            android:color="#333333" />
    </shape>

</shape>

select_button_selected.xml

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

    <gradient
        android:angle="270"
        android:endColor="#AAAAAA"
        android:startColor="#BBBBBB"
        android:type="linear" />

    <corners android:radius="10dp" />

    <shape>
        <stroke
            android:width="1dp"
            android:color="#333333" />
    </shape>

</shape>
the-ginger-geek
  • 7,041
  • 4
  • 27
  • 45
1

Hi I have already posted the answer for you in this question link android - identify items in a ListView, you said it hasn't worked for you, I have provided the same answer and it worked for other check this question Inflate ListView row from OnClickListener in Android?. I hope this will help you.

Community
  • 1
  • 1
Pragnani
  • 20,075
  • 6
  • 49
  • 74