How to fire onListItemClick in Listactivity with buttons in list?

CodeFusionMobile picture CodeFusionMobile · Nov 30, 2009 · Viewed 64.8k times · Source

I have a simple ListActivity that uses a custom ListAdapter to generate the views in the list. Normally the ListAdapter would just fill the views with TextViews, but now I want to put a button there as well.

It is my understanding and experience however that putting a focusable view in the list item prevents the firing of onListItemClick() in the ListActivity when the list item is clicked. The button still functions normally within the list item, but when something besides the button is pressed, I want onListItemClick to be triggered.

How can I make this work?

Answer

Ewoks picture Ewoks · Sep 21, 2012

as I wrote in previous comment solution is to setFocusable(false) on ImageButton.

There is even more elegant solution try to add android:descendantFocusability="blocksDescendants" in root layout of list element. That will make clicks onListItem possible and separately u can handle Button or ImageButton clicks

Hope it helps ;)

Cheers