Android : ListView with Buttons -> OnItemClick do nothing
Asked Answered
V

4

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...

Vetter answered 8/6, 2011 at 6:51 Comment(5)
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?Guideboard
Having buttons in a listview sounds weird. Have you thought about using a context menu instead?Fransen
@Stuti: And I can't get your writing. Could you please write "you" instead of "u"? We should keep the language clean here. ThanksAbsolve
@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.Guideboard
@Guideboard 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 acceptWheelock
C
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.

Cripple answered 24/10, 2012 at 6:26 Comment(2)
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.Fluellen
Thank you very much!!! For readers: for the Button it is a true solution, I check it.Pavo
W
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).

Wheelock answered 8/6, 2011 at 7:28 Comment(0)
I
2

The reason is button in your listview absorbs the onItemClickEvent.

A well explained tutorial is here

Ician answered 14/2, 2012 at 9:46 Comment(0)
U
0

You can use this in .setOnItemClickListener of the ListView

view.findViewById(R.id.btn_id).setOnClickListener(new View.OnClickListener(){//your method})
Unreasoning answered 8/6, 2011 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.