Home > Tricks & Tips > ListView doesn’t respond to OnItemClickListener

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

About these ads
  1. Kononjump
    December 31, 2013 at 7:23 pm

    Realy big thanks to you!

  2. September 10, 2013 at 7:31 pm

    really great ans.. I am very thank full to you.. nice tutorial man.. no words to say … keep posting…

  3. Mahmoud El-Maghraby
    August 23, 2013 at 11:21 pm

    Awesome post, I got my problem solved :)

  4. July 13, 2013 at 7:14 pm

    2. Scenario
    work :D

  5. rupeshsasne
    May 17, 2013 at 12:10 am

    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,

  6. Tmr
    May 15, 2013 at 2:20 pm

    I lost my full day without visiting your page. great post

  7. Manu
    March 24, 2013 at 12:32 am

    THANK YOU! I was looking hundreds of pages without success, but your solution worked like a charm!

  8. Chaim
    February 17, 2013 at 8:49 pm

    Thank you!

  9. keivn
    February 14, 2013 at 11:48 pm

    thanks a lot

  10. Paul Kim
    December 27, 2012 at 3:14 pm

    Thank you I love your advice!!!

  11. Jorge
    December 4, 2012 at 3:14 am

    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 :( ….

  12. user
    December 3, 2012 at 2:42 pm

    Thanks a ton. :)

  13. Shahab Ahmad
    November 29, 2012 at 6:01 pm

    very nice…work Thanks a lot!!!!!!!!!!!

  14. governmentauctions
    July 19, 2012 at 8:20 pm

    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!

  15. BMSDROID
    June 8, 2012 at 12:56 pm

    i tried all the above but not able to fire onItemClick on ListView. I inflated two ImageViews,three TextViews

  16. Faiz
    May 24, 2012 at 2:03 pm

    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.

  17. gaurav shukla
    May 23, 2012 at 3:32 pm

    Help me allot…… thanks u so so so much…..

  18. Thanks!
    February 16, 2012 at 9:13 am

    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

  19. Thanks!
    February 16, 2012 at 9:08 am

    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?

  20. Thanks!
    February 16, 2012 at 9:08 am

    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?

  21. Juan
    February 3, 2012 at 11:37 pm

    Thanks, I was in case #1 I had a work arond for this but now I know the real cause of the issue.

  22. Meghana
    January 30, 2012 at 8:25 pm

    Thanks, worked for me! Saved a lot of my time :) Great work !!!

  23. mav3656
    January 25, 2012 at 10:58 pm

    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 !

  24. rooban abraham
    January 10, 2012 at 8:49 pm

    Nice post , really helped me :)

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: