HardCoder
HardCoder

Reputation: 3036

My listview does not get any clicks anymore when I add a checkbox

I created a listview with an image and some text with following layout and it worked. Whenever I clicked the Text "onListItemClick" was called successfully:

<?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:orientation="horizontal">

<ImageView
android:id="@+id/image"
android:layout_width="40dip" 
android:layout_height="40dip"
android:src="@drawable/icon"/>

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>

Then I added a checkbox between the text and the image and I could no longer get the clicks when I click on the text and also not when I click the checkboxes:

<?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:orientation="horizontal">

<ImageView
android:id="@+id/image"
android:layout_width="40dip" 
android:layout_height="40dip"
android:src="@drawable/icon"/>

<CheckBox
android:id="@+id/check"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>

Is there a way so that I can get clicks on the checkboxes and the text ?

Upvotes: 2

Views: 369

Answers (2)

alex stewart
alex stewart

Reputation: 33

I believe it has to do with the focusable attribute of the checkbox try setting it to android:focusable="false" in the layout

Upvotes: 1

Sebastian Olsson
Sebastian Olsson

Reputation: 836

The checkbox is probably stealing focus.

Try setting android:focusable="false" on the checkbox.

A side note: Please note that there is a way to handle checkbox selects in lists where the framework takes care of most of the job. Set CHOICE_MODE_MULTIPLE on the list and use a layout that has for example a CheckedTextView. You can get ids using getCheckedItemIds(). See for example android.R.layout.simple_list_item_multiple_choice . There are implementations in the api demos. Don't try this on expandable list view though, since that won't handle the click ids correctly.

Upvotes: 4

Related Questions