Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView and Button in each row and onListItemClick()

Tags:

android

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!

like image 665
Lukas Fryc Avatar asked Aug 31 '10 17:08

Lukas Fryc


2 Answers

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.

like image 82
Jorgesys Avatar answered Sep 24 '22 03:09

Jorgesys


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?

like image 38
Cheryl Simon Avatar answered Sep 25 '22 03:09

Cheryl Simon