15

I have an item layout like this, and set the background by using item selector

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="@drawable/itemselector"
android:orientation="horizontal" >
<CheckBox
    android:id="@+id/message_row_checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/message_row_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold"
        android:textColor="@color/black" />

itemselector.xml :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item 
 android:state_pressed="true" 
 android:drawable="@color/yellow" />
<item 
 android:state_selected="true" 
 android:drawable="@color/green" />
<item 
 android:drawable="@color/white" />
</selector>

I have a ListView which will content some items. Then I used setOnItemClickListener() but it doesn't work. I found that the if I remove the checkbox in the item, everything will be okey.

What was the problem between the checkbox and the listener here? Can you give me some solution?

Update : This is the code of listener

mainListView.setAdapter(messageAdapter);
mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                        Message p = (Message) arg0.getItemAtPosition(arg2);
                        Toast.makeText(TarsiusActivity.this, p.getTitle(), Toast.LENGTH_LONG);
                        Log.i("Item Clicked", p.getTitle());
                    }
});

ps: I want to make the inbox like gmail on android. Each row has a checkbox and user can click on item if they want to see the message

TrungNguyen
  • 175
  • 1
  • 2
  • 8
  • 1
    Please show the code you used to set the `onClickListener()`. – PearsonArtPhoto Mar 28 '12 at 03:56
  • possible duplicate of [ListView OnItemClickListener Not Responding?](http://stackoverflow.com/questions/2367936/listview-onitemclicklistener-not-responding) – Heath Borders Apr 12 '14 at 04:14
  • possible duplicate of [OnItemClickListener doesn't work with ListView item containing button](http://stackoverflow.com/questions/8413656/onitemclicklistener-doesnt-work-with-listview-item-containing-button) – Narkha May 15 '15 at 12:18

5 Answers5

45

Best way to do this is set these following properties for your checkbox:

  android:focusable="false"
  android:focusableInTouchMode="false"

I had a same issue and did this.

silentkratos
  • 941
  • 2
  • 11
  • 9
18

The onItemClickListener for the listView will not work if you have any clickables like buttons, ImageButton, Checkbox, etc in the listView. Add

mainListView.setItemsCanFocus(true);

Refer ListView OnItemClickListener Not Responding?

Community
  • 1
  • 1
user936414
  • 7,574
  • 3
  • 30
  • 29
9

Just add

android:descendantFocusability="blocksDescendants"

To your top level LinearLayout of the listitem.

Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55
brendan
  • 1,705
  • 1
  • 18
  • 24
1

Use setOnCheckedChangeListener instead of onItemClickListner for checkbox

CheckBox check;
check=new CheckBox(this);
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
    }
});
assylias
  • 321,522
  • 82
  • 660
  • 783
Pradeep Sodhi
  • 2,135
  • 1
  • 19
  • 19
  • is there any other solution? I want to make it look like gmail on android. Still have the checkbox and if user click on the item if they want to read the message – TrungNguyen Mar 28 '12 at 04:17
1

You could add this code within your OnItemClickListener method:

public void onItemClick(AdapterView parent, View view, int position, long id){
   CheckBox box = (CheckBox)view.findViewById(R.id.course_search_checkbox);
   box.setChecked(true);
}
Ant4res
  • 1,217
  • 1
  • 18
  • 36
Pradeep Sodhi
  • 2,135
  • 1
  • 19
  • 19
  • thanks for your solution but did you use GMAIL on android? :) – TrungNguyen Mar 28 '12 at 04:32
  • 1
    yup... in the inbox, if you clicked on the checkbox there will be some option for you BUT if you clicked on the rest it will open the message.. What was what I want ;) – TrungNguyen Mar 28 '12 at 04:37
  • firstly you have to make your options view in your same xml whose alignment is parentbottom. In onCreate of your activity set visibility of whole the option view to gone.Next when you click on checkbox then set visibility of your option view to visible in your onItemClick. – Pradeep Sodhi Mar 28 '12 at 04:43
  • This is not proper solution because after checked listitem it can not remove checkbox value – Arpit Patel Jun 16 '16 at 04:13