Search code examples
androidandroid-listviewactionbarsherlock

ListView.setChoiceMode and custom list item


In a SherlockListFragment I'm using android.R.layout.simple_list_item_activated_1 to display a single line of text. I'm using the following to get the ListView to automatically highlight the list item when selected...

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

This works fine.

However, I have another SherlockListFragment which uses a list item layout with two TextView widgets as follows...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">
    <TextView
        android:id="@+id/gl_item_single_start_time_short"
        ...
        >
    </TextView>
    <TextView 
        android:id="@+id/gl_item_single_title"
        ...
        >
    </TextView>
</RelativeLayout>

What I'd like to do is have the second TextView (gl_item_single_title) automatically highlighted when the list item is selected.

The question is - what do I have to do to make this happen? Is there an attribute I can set on that TextView or do I have to handle this myself for the view returned by the adapter?


Solution

  • I'm using android.R.layout.simple_list_item_activated_1 to display a single line of text.

    I assume that you meant that you are using your own layout copied from android.R.layout.simple_list_item_activated_1, since that resource only exists on API Level 11+.

    What I'd like to do is have the second TextView (gl_item_single_title) automatically highlighted when the list item is selected.

    I assume that you mean that you want it to behave as android.R.layout.simple_list_item_activated_1 does, with the persistent highlight for your selection.

    what do I have to do to make this happen?

    Copy android.R.layout.simple_list_item_activated_2 and modify to suit. Or, use android:background="?android:attr/activatedBackgroundIndicator" on some other layout. Note, though, that this only works on API Level 11+.

    If you want something that works for all API levels, though:

    First, extend your app's theme with one, in a res/values-v11/ directory, that defines some new style (e.g., activated):

    <style name="activated" parent="android:Theme.Holo">
      <item name="android:background">?android:attr/activatedBackgroundIndicator</item>
    </style>
    

    Then, in res/values/, define that same style to be a no-op:

    <style name="activated">
    </style>
    

    Finally, apply that style to your row layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:padding="2dip"
      android:minHeight="?android:attr/listPreferredItemHeight"
      style="@style/activated"
    >
      <ImageView android:id="@+id/flag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|left"
        android:paddingRight="4dip"
      />
      <TextView android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|right"
        android:textSize="5mm"
      />
    </LinearLayout>