Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CheckBox Making listView item non-clickable

I have a task list, and have defined each list item in list_item.xml as follows

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/list_overdue_pic"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/overdue"
    android:visibility="invisible" />

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dp" >

    <CheckBox
        android:id="@+id/list_done"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:paddingRight="5dp"
        android:gravity="center_vertical" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/list_done"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/list_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="10dp"
                android:textColor="@color/white"
                android:textSize="18dp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/list_note_pic"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_toRightOf="@+id/list_title"
                android:background="@drawable/note" />
        </RelativeLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/list_prio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="17dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/list_due_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="17dp"
                android:visibility="gone" />

            <TextView
                android:id="@+id/list_calendar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="17dp"
                android:visibility="gone" />
        </LinearLayout>

        <TextView
            android:id="@+id/list_note"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="15dp" />
    </LinearLayout>
</RelativeLayout>

The resulting list item looks like this:

item

The problem is that with the CheckBox in the xml, I am unable to select any list item. Clicking on the list item should call onListItemClick however it does nothing. If I add

android:visibility="gone"

To the CheckBox, I am able to click items as I please. Why is the CheckBox ruining this ability?

like image 521
finiteloop Avatar asked Dec 18 '11 04:12

finiteloop


1 Answers

The key to solving this is to add the

 android:focusable="false"

attribute to the CheckBox. Once this is added, the CheckBox will stop stealing the clickibility from the list items, and you will be able to both tap the CheckBox, and select individual list items.

like image 120
finiteloop Avatar answered Sep 27 '22 21:09

finiteloop