10

My problem come from ListView. Without Buttons, the .setOnItemClickListener of the ListView work well. (start an Activity)

At time that I add Buttons into items of the ListView, Buttons are clickable but the items aren't more clickable...

I try to use myListView.setItemCanFocus(true); but it don't work...

Gregco
  • 111
  • 1
  • 1
  • 3
  • I can not get ur question. Buttons are clickable, when u add onCLickListener and items when setOnItemClickListener. Can u explain more precisely what u have added and where? – Stuti Jun 08 '11 at 06:55
  • Having buttons in a listview sounds weird. Have you thought about using a context menu instead? – Philip Sheard Jun 08 '11 at 06:56
  • @Stuti: And I can't get your writing. Could you please write "you" instead of "u"? We should keep the language clean here. Thanks – WarrenFaith Jun 08 '11 at 21:17
  • @WarrenFaith: I am very sorry. Actually I am very used to it. But I will keep it in mind and try my best to avoid it. Thanks. – Stuti Jun 09 '11 at 04:29
  • @Stuti another thing - if you accept an answer you will increase your rating, then you can do more. You get an additional 5 points for each answer you accept – Stev_k Jun 14 '11 at 19:59

4 Answers4

37

Remove the focusable attribute from the Button would solve this problem. You could do that either in a layout xml file or java source code.

And one more tip, if you are using ImageButton instead of Button, you need setFocusable in your java code to make that work, cause the constructor of ImageButton would enabe this attribute after inflate from xml file.

liuyong
  • 987
  • 10
  • 19
  • This is great. Way quicker than the other work arounds. Thanks! For anyone who doesn't know how to do this, just add android:focusable="false" to your button in your XML file. – Scott Kohlert May 10 '14 at 17:09
  • Thank you very much!!! For readers: for the Button it is a true solution, I check it. – DenisMath Jan 12 '15 at 13:06
2

It might be better to use an onTouch() callback for the clickable button within the listview. You should then be able to click.on both the list item and the button. See this question for some code (no need for touchDelegate).

Community
  • 1
  • 1
Stev_k
  • 2,118
  • 3
  • 22
  • 36
2

The reason is button in your listview absorbs the onItemClickEvent.

A well explained tutorial is here

Ads
  • 6,681
  • 12
  • 47
  • 72
0

You can use this in .setOnItemClickListener of the ListView

view.findViewById(R.id.btn_id).setOnClickListener(new View.OnClickListener(){//your method})
Jaydeep Khamar
  • 5,975
  • 3
  • 32
  • 30