ListView and Buttons inside ListView
Asked Answered
S

5

60

I want to display a Button inside the ListView. The goal should be to click on the ListView line or on the button.

Click on the line it shows more info. Click on the button it shows at the bottom more buttons.

The same as the GMAIL app.

On the right there is a checkbox and after clicking on the checkbox at the bottom, the button bar appears.

My problem is after inserting the button into the ListView, the button is not clickable. If I add the to the LinearLayout from the button llButton.setClickable() it works. But, only the button. The ListView itself doesn't react on clicks anymore!

I have tried this example.

The same issue as above...

Ssw answered 15/6, 2010 at 14:2 Comment(5)
Did you try using the Context Menu for the listview?Kodok
this might helpHeida
Yes i have a context menu, but i want to add a checkbox too.Ssw
Maybe my answer on this question may help.Rainier
"If I add the to the LinearLayout from the button", please fix this sentence. I can't understand it.Ogbomosho
S
9

If you are using a custom Adapter the Button click inside a ListView will not work so you should try to use the following code to check for OnItemClickListener.

listId.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
      // Your code for item clicks
   }
});
Shaefer answered 12/2, 2011 at 6:31 Comment(2)
The answer provided by teh1 below is better.Wheeling
@Shaefer plz look over this link #12346981Delaine
D
122

Just to make this clear – and no one seems to have said something this simple – whilst one is not allowed to have a focusable button work in conjunction with the list view, there is a much simpler solution for this.

The accepted answer is a given - you should always do that when setting the click listener for list items, so that is silly that OP didn't know that.

If you are using an XML layout as your list item, simply set the button to have the following attribute and it will cause the list item to be clickable as well:

android:focusable="false"

Disinclined answered 10/5, 2012 at 7:19 Comment(7)
Thank you, it fixed a problem I was facing!Selfknowledge
Strangely, this trick does not work with ImageButton. Know of any that do?Achromic
I guess it shouldn't be strange, seeing as ImageButton extends ImageView, while Button extends TextView. With ImageButton. this trick does work, but must be set after changes to visibility. (That was my issue).Achromic
Did not know. Thanks for extending my answer.Disinclined
@iPaulPro how exactly did u get it working with an ImageButton? Im having the same issue. It works with a simple button, but not with imagebuttonBlessing
@arnab321 If you are programmatically changing the visibility of the ImageButton you must call setFocusable(false) after the visibility change.Achromic
ImageButton solution that works for me: https://mcmap.net/q/330249/-can-39-t-click-on-listview-row-with-imagebuttonCestode
H
20

Add the line below to your list item XML.

android:descendantFocusability="blocksDescendants"

Then your list item will be like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:descendantFocusability="blocksDescendants"
android:layout_height="wrap_content" >

    // Your layout objects here

</RelativeLayout>
Hicks answered 22/3, 2014 at 20:47 Comment(1)
This is the only answer that fixed my problemTuneful
S
9

If you are using a custom Adapter the Button click inside a ListView will not work so you should try to use the following code to check for OnItemClickListener.

listId.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
      // Your code for item clicks
   }
});
Shaefer answered 12/2, 2011 at 6:31 Comment(2)
The answer provided by teh1 below is better.Wheeling
@Shaefer plz look over this link #12346981Delaine
D
7

To have the event be triggered when either the button or the list item is clicked, you can do the following:

You handle only onItemClick:

mListView.setOnItemClickListener(new ListView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> a, View v, int i, long l) {
        // handle click here
    }
);

In the adapter you modify the button to not be clickable/focusable (or do this in the xml file instead):

public class MyAdapter extends BaseAdapter {
    ...
    public View getView(int position, View convertView, ViewGroup parent) {
        .....  
        Button btn = view.findViewById(R.id.button);
        btn.setFocusable(false);
        btn.setClickable(false);               
    }
}
Duran answered 26/9, 2012 at 7:51 Comment(0)
E
0

In my case i had to add this attribute in the listView :

<ListView
...
android:clickable="true"
...
</ListView>

And in the adapter just add on click listener in the button view.

wrapper.getButtonHi().setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            DebugUtils.logDebug("Clickeado :: "+ mContact.getUserId());
        }
});

Its important to set final the variables:

public View getRowView(final int position, View convertView, ViewGroup parent) {
    final BrowseContactItemWrapper wrapper;
    final UserModel mContact = lstContact.get(position);
    .....
}
Euphemism answered 30/9, 2014 at 18:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.