Creating Email-style ListView with checkboxes and ActionBar integration

I want to build a ListView similar to what you see in email apps. Each row should have a checkbox and two text fields stacked vertically. I need to handle clicks on the checkbox separately from clicks on the rest of the row. The ActionBar should update when items are selected and show options like “Select All” and “Clear Selection”.

Here’s my current layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <CheckBox android:id="@+id/itemCheckbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" />

    <LinearLayout android:id="@+id/contentContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="8dp"
        android:clickable="true"
        android:focusable="true">

        <TextView android:id="@+id/subjectText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp" />

        <TextView android:id="@+id/timestampText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="12sp" />

    </LinearLayout>

</LinearLayout>

The layout displays correctly but I’m stuck on the click handling. I set up a callback class that implements ListView.MultiChoiceModeListener and set the choice mode to CHOICE_MODE_MULTIPLE_MODAL. However, I can’t figure out how to connect my custom checkbox with this system.

I’m using a custom CursorAdapter to populate the list from a SQLite database. I think the click listeners need to be set up in the adapter’s newView() and bindView() methods, but my attempts haven’t worked.

Has anyone successfully implemented this email-style selection behavior? Most examples I find use the default layouts, but I need custom views.

I hit the same issues building a messaging app last year. The problem is CHOICE_MODE_MULTIPLE_MODAL wants to control selection itself, but you’re using custom checkboxes - they fight each other. I ditched the modal choice mode completely and handled selection manually. In your adapter, use a SparseBooleanArray to track selected positions. Set OnCheckedChangeListener on your checkbox in bindView() - when it’s toggled, update your array and call notifyDataSetChanged(). For ActionBar integration, create a custom ActionMode and start/finish it based on selection count. Override onCreateActionMode() to inflate your menu with ‘Select All’ and ‘Clear Selection’ options. In onActionItemClicked(), handle these by updating your selection array. Make content container clicks toggle the checkbox programmatically - this triggers the OnCheckedChangeListener. You get full control over selection behavior while keeping that email-like experience users expect.

Checkbox handling with cursors is tricky - I’d keep it simple. Use setTag() on your checkbox with the cursor position in bindView(). When the checkbox changes, grab the position from getTag() and update your selection tracking. For the actionbar, just check if anything’s selected after each change and show/hide it. Works way better than fighting ListView’s built-in selection modes.

You’re overcomplicating this with CHOICE_MODE_MULTIPLE_MODAL. I built something similar for a file manager and here’s what worked: make the checkbox your main selection tool, not ListView’s built-in stuff. In bindView(), set checkbox state from your data model. Use a HashSet for selected item IDs from your cursor. When someone checks a box, add/remove the ID and update ActionBar visibility right away. For content clicks, just toggle the checkbox - your OnCheckedChangeListener handles the rest. ActionBar’s easy now - show when HashSet has items, hide when empty. ‘Select All’ just loops through your cursor, adds all IDs to the set, then calls notifyDataSetChanged(). This gives you full control over selection state during scrolling and orientation changes, which ListView’s selection handling sucks at with custom layouts.