11

I have a ListView with some elements on it. Each row has a TextView and a Button. It looks like this:

| Some text in a row (Button) |

Now, when I click on this text nothing happens. Simply no one function is called. But when I click on the button I can handle the event. I use onListItemClick()

So what should I use instead of this TextView to be able to handle an event (when I click on the text)?

Before this I had only one TextView in each row and when I was clicking on a row everything worked fine (onListItemClick() was called).

Thank you in advance!

Lukas Fryc
  • 1,730
  • 3
  • 16
  • 18

2 Answers2

9

add the property focusable="false" to your TextView ::

<TextView
...
...
        android:focusable="false"
        />

and probably you will need to do the same to the other elements inside your ListView.


Programatically we can use the method setFocusable():

v.findViewById(R.id.my_button).setFocusable(false); 

setFocusable(): Setting this to false will also ensure that this view is not focusable in touch mode.

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 1
    Though this works fine, you should consider implementing the following solution, as this will make d-pad/trackball navigation behave correctly: http://stackoverflow.com/questions/3789943/using-android-how-can-i-select-rows-from-a-listview-which-contains-button-contro/3791340#3791340 – sven Oct 17 '10 at 11:51
  • 2
    this will work for textview, but for imagebutton, maybe you need, put on code `v.findViewById( R.id.my_btn ).setFocusable( false );` at Adapter getView. – Ratata Tata Feb 21 '13 at 19:59
  • @OrlandoLeite, thanks, saved my bacon, I added the ``focusable="false"`` and it didn't do the trick, but the ``setFocusable`` did do the trick !!! – Mairyu Apr 05 '18 at 22:03
  • @Mairyu lill bit weird, setFocusable supossed to be the same but programatically. – Jorgesys Apr 05 '18 at 22:06
6

The challange is that the ListView and the button fight for focus. Typically, only one of the two can receive focus (and thus be clicked). In your cause, the button is the focusable one.

To adjust this, you can play with the descendantFocusability property of the ListView.

What are you trying to accomplish by having a button within a ListView item? Do you want something different to happen when you click on the button, vs when you click on the listview item outside the button?

Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
  • 1
    yea, just as you've said I want the button to do different task. Thank you for explaining. – Lukas Fryc Aug 31 '10 at 18:49
  • 1
    But still I have some problems. Like you said I want to do different thing when I click button vs when I click on the ListView outside the button. The onListItemClick() is only called when I click somewhere on the list but not on the buttons. I thought I would get the View of the button from parameter "v" from the onListItemClick(). So, what you would suggest me to do so that I can handle the event of clicking on the particular button? – Lukas Fryc Aug 31 '10 at 19:43
  • 1
    This guy: http://stackoverflow.com/questions/2679948/focusable-edittext-inside-listview has a solution to a similar problem that might be applicable... Keep in mind though, that people have bad aim when trying to tap on small buttons. Generally, people will expect to be able to click anywhere in the row of a ListView, since thats how most apps work. You might want to rethink your UI to avoid having nested clickable areas. – Cheryl Simon Aug 31 '10 at 20:13
  • 2
    http://stackoverflow.com/questions/500264/android-multiple-actions-on-a-list-view-focus-issue This helps me. Thank you for notice the way people use such apps. Generally, its better to use e.g context menu in such situations. – Lukas Fryc Aug 31 '10 at 21:05