33

I have ListView with custom Adapter which supplies View to ListView in this way:

   public View getView(int position, View convertView, ViewGroup parent)
   {
        RelativeLayout.LayoutParams lineParams;
        RelativeLayout line=new RelativeLayout(context);

        TextView tv=new TextView(context);
        tv.setText("Text in postion="+i);
        lineParams=new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lineParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        line.addView(tv, lineParams);
        lineParams.addRule(RelativeLayout.CENTER_IN_PARENT);

        //checkbox
        CheckBox checkBox=new CheckBox(context);
        lineParams=new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lineParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        lineParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        line.addView(checkBox, lineParams);
        return line;
    }

And somewhere inside ListView there's setOnItemClickListener(), which should intercept item clicking events. My problem that, whenever I try to add checkbox to item - I don't get any responces from my ListView. If I skip CheckBox or any other Button it works.

I am really stuck with this problem, I have tried all kind of Layouts, aligning, wrapping and so on - useless. Looks like CheckBox interferes ListView item click events.

Any ideas how to overcome?

Barmaley
  • 16,638
  • 18
  • 73
  • 146

8 Answers8

54

just add this line into the item views instead of listView itself

android:focusable="false"

check more detail about this from Android custom ListView unable to click on items

Community
  • 1
  • 1
Pratik
  • 30,639
  • 18
  • 84
  • 159
  • 1
    It doesn't help! Any other options? – Barmaley Dec 07 '11 at 10:46
  • 5
    OK, I've figured - it works. I would need to put setFocusable(false) for item views instead of listView itself. Thanx! – Barmaley Dec 07 '11 at 12:21
  • 5
    It does work if setting focusable false on the view stealing the focus - in this case the check box or button. – r1k0 Apr 10 '12 at 16:00
  • If you have textview in each row then make sure `textIsSelectable` is set to `false` – user427969 Apr 21 '13 at 04:50
  • In my Adapter, I set `btn.setFocusable(false)` which solves the problem. I think Android sets `focusable` to true after loading the XML. – Sufian Jul 01 '14 at 08:13
  • Appears this solution doesn't work when ImageButton is used in the list item – Bob Nov 12 '14 at 10:41
19

If you have ImageButtons inside the list item, you need to add:

android:descendantFocusability="blocksDescendants"

to the root list item element [such as the root layout].

Then within each ImageButton in the list item, you need to add:

android:focusableInTouchMode="true"

This worked for me - but I was using ImageButtons, not the standard button.

dandev91
  • 1,691
  • 3
  • 22
  • 34
13

I have also faced the same issue I have tried to set android:focusable="false" to listview but it don't work then I add this to listview item.. like in my listview item I have uesed Toggle button which was creating problem, I add android:focusable="false" to Toggle button and listview on item click listener start work again

Jaiprakash Soni
  • 4,100
  • 5
  • 36
  • 67
1

Add following line to your listView

android:choiceMode="singleChoice"

or make sure to set following lines to your layout text fields

android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
GameBug
  • 136
  • 2
  • 10
0

I had also had the problem of a Button in my ListView. Unfortunately just setting the focus to false for all objects in my Adapter did not work for me.

I now have a workaround.

In your Adapter create an OnClickListener for the button (or other clickable object) if you have not already done that. In that OnClickListener you call the OnItemClickListener yourself.

public void onClick(View v) {
    mOnItemClickListener.setOnItemClick(mListView, v, vPos, vId);
}

It does mean that you will need to give your adapter access to both the parent ListView and the OnItemClickListener.

Vincent Ketelaars
  • 1,069
  • 1
  • 14
  • 35
0

You can consider to write your on OnTouchEvent in your listview item and send the proper touchEvent to you child view , the button .

Lion
  • 81
  • 5
0

Well i know none of the above solutions will work.I tried changing xml attributes but those does not work out, But i implemented it in a new fashion. Here is how:

Create an interface CheckBoxOnCheckListener with method onCheckBoxChecked and pass needed parameters, implement interface CheckBoxOnCheckListener in your activity or fragment containing listView.

Next in your adapter, declare an mListener as CheckBoxOnCheckListener, and pass this as a parameter to Adapter's constructor from fragment/activity and cast it to CheckBoxOnCheckListener and assign to mListener.

Next set mListener as itemView.onClick or CheckBox.onCheckCheckedListener and onCheckChanged method call mListener.onCheckBoxChecked.

That's it. It will definitely work,it worked for me. For code just pm.

Amit
  • 105
  • 1
  • 6
-6

If you are using ListView in Activity, ensure you have setup setOnItemClickListener()

myListView.setOnItemClickListener(this); // if your activity implement OnItemClickListener
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
grzesl
  • 1