ListView doesn’t respond to OnItemClickListener
There will be case that your custom list item doesn’t respond when you click…so what’s the reason and what’s the solution?
Here several problems and solutions:
1. Scenario: list item layout contains CheckBox
Problem: OnItemClickListener doesn’t respond.
Reason: CheckBox is also having its own click listener by default to change its state, and it overrides the container ListView.
Solution: remove focus on CheckBox by setting these attributes to “false”
android:focusable="false"
android:focusableInTouchMode="false"
2. Scenario: random
Problem: OnItemClickListener just doesn’t repond any at all!!!!
Reason: No idea..
Solution: in code, just set OnItemClickListener before setting Adapter. It works randomly @@!
3. Scenario: list item contains ImageButton
Problem: OnItemClickListener just doesn’t repond any at all!!!!
Reason: No idea!!!
Solution: in code, set ImageButton focus to “false”
ImageButton button = (ImageButton) convertView.findViewById(R.id.imageButton); button.setFocusable(false);
4. Scenario: list item contain TextView
Problem: OnItemClickListener just doesn’t repond.
Reason: I think you have set this attribute to TextView: android:inputType=”textMultiLine”
Solution: just remove that attribute, using android:minLines/android:maxLines instead.
5. Scenario: list item contain a TextView that is linked to website URL or whatever “mailto:” things
Problem: OnItemClickListener just doesn’t repond.
Reason: the TextView overrides focus of list item.
Solution: just remove attribute “android:autoLink” on TextView.
Hope you solve your problems!
Cheers,
Pete Houston
Leave a Reply Cancel reply
Calendar
Categories
- Home (4)
- Learning (149)
- Books & Materials (2)
- Design Patterns (2)
- PhoneGap (1)
- Tricks & Tips (75)
- Tutorials (69)
- Of Diary (19)
- Pete's Works (8)
- Applications (7)
- Publications (1)
- Tools (7)
Recent Posts
Most Popular
Tag Cloud
Archives
- January 2013 (5)
- December 2012 (4)
- October 2012 (2)
- September 2012 (1)
- August 2012 (2)
- July 2012 (12)
- June 2012 (1)
- May 2012 (7)
- March 2012 (1)
- February 2012 (3)
- January 2012 (2)
- December 2011 (8)
- November 2011 (12)
- October 2011 (19)
- September 2011 (16)
- July 2011 (29)
- June 2011 (63)
Statistics
- 546,784 View
Hi Pete,
I have applied your instructions to following code.
Here I have added `onClickListener` and `setFocusable(false)` to root layout of a list item, please have a look at following,
this is a part of list item layout “list_item”.
[..Some code..]
and the getView of adapter has following code,
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item,
null);
convertView.setOnClickListener(onClickListener);
convertView.setFocusable(false);
[..Some code..]
}
Still the list view’s `onItemClick` method is not getting called.
Any solution?
Thanks,
I lost my full day without visiting your page. great post
THANK YOU! I was looking hundreds of pages without success, but your solution worked like a charm!
Thank you!
thanks a lot
Thank you I love your advice!!!
Pete for me doesn’t works! Pls help me … I use a class extends BaseAdapter and then I inflate a ListView from Fragment …. Finally, I want to use a OnItemClickListener … and thats method not responding
….
Thanks a ton.
very nice…work Thanks a lot!!!!!!!!!!!
Gday!Now I am at work surfing around your website by my own new iphone! Only just need to mention I really enjoy studying your own www and also start looking in front to all of your writes! Keep up the actual superb work!
i tried all the above but not able to fire onItemClick on ListView. I inflated two ImageViews,three TextViews
there is another case also. if your view contains table layout as root element then onItemClickListener will not respond. just add android:clickable=”false” to table layout.
Great to know! Thanks for sharing
Help me allot…… thanks u so so so much…..
I found the reason for Scenario 3.
There is a feature of the ImageButton in Android: it cannot be set to non-focusable using xml attribute, because someone explicitly calls setFocusable(true) in the constructor: see source***. There is an issue submitted, but it seems that the Android team will not fix it: https://review.source.android.com/15203
***http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/widget/ImageButton.java;h=12a68db8af1e8794bb19f41a1f275c860b9ca30c;hb=HEAD
Thank you!! The Scenario 3 fix worked perfectly for me. Who would have thought that setting focusable to false programmatically would work when setting it in XML didn’t?
Thank you!! Scenario worked perfectly for me. Who would have thought that setting focusable to false programmatically would work when setting it in XML didn’t?
Thanks, I was in case #1 I had a work arond for this but now I know the real cause of the issue.
Thanks, worked for me! Saved a lot of my time
Great work !!!
Thank’s a lot ! was in scenario 3 !
In xml, doesn’t work to set focusable to false. But in code, you are right. That solved my problem. Thanks !
Nice post , really helped me