ListView with multiple buttons, list item can't be clicked
Asked Answered
O

3

6

I have a list with two buttons in it. When I want to click a list item it doesn't work, but my button is still clickable. How I can make all buttons include the entire list item to be clickable?

List item:

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:mode="twoLine">
       
              <Button
                    android:id="@+id/erase"
                    android:layout_width="40dip"
                    android:layout_height="40dip"
                    android:focusable="false"
                    android:focusableInTouchMode="false"/>
              <ImageButton android:id="@+id/soundf"
                    android:layout_width="40dip"
                    android:layout_height="40dip"
                    android:focusable="false"
                    android:focusableInTouchMode="false"/> 
              <TextView android:id="@+id/texxt1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#CC0"/>
</TwoLineListItem>

Layout containing the ListView:

    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">
        <Button android:id="@+id/left" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:text="English to Indonesia"
            android:layout_weight="1" 
            android:background="@drawable/chbutt" />
        <Button android:id="@+id/right" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:text="Indonesia to English"
            android:layout_weight="1" 
            android:background="@drawable/chbutt" />
    </LinearLayout>
    
    <ListView android:layout_height="wrap_content"
        android:layout_width="fill_parent" 
        android:id="@+id/history"
        android:headerDividersEnabled="false"
        android:footerDividersEnabled="false"
        android:isScrollContainer="false" />
</LinearLayout>
Obolus answered 31/5, 2011 at 7:25 Comment(3)
can you provide you xml where you have defined listviewPyrargyrite
Duplicate of #2322890Ciccia
hi @Deepak, you can see it nowObolus
J
29

For Buttons, Checkboxs and ImageViews:

android:focusable="false"

Now both (buttons and rows) of ListView are clickable.

For ImageButtons, you have to set focusable while running, because the constructor of ImageButtons sets it to true. I recommend you using a ImageView instead of a ImageButton.

Jer answered 23/3, 2012 at 15:15 Comment(7)
I've been agonizing over this issue for some time. Thank you very much sir.Cockneyfy
Thanks, this topic used to give me a couple of headaches! Its a little bit trick.Jer
how will you identify whether the click is from listitem or the button??Schappe
you'd be called on the listener for the list or the button respectivelyJer
Thanks for a hint regarding the constructor of ImageButtons!Hammer
More details about the reason, thanks! Please give some references.Roderic
Use ImageView works for me without handle the focus property in some way.Sore
D
0
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout  android:id="@+id/rel1" android:layout_width="fill_parent"
        android:layout_height="wrap_content">

<Button
        android:id="@+id/erase"
        android:layout_marginLeft="6dip"
        android:layout_marginTop="6dip"
        android:layout_width="40dip"
        android:layout_height="40dip"
        android:background="@drawable/closebtn"
        android:focusable="false"
        android:focusableInTouchMode="false"
    />
   <ImageButton android:id="@+id/soundf"
        android:layout_width="40dip"
        android:layout_height="40dip"
        android:layout_below="@+id/erase"
        android:layout_alignLeft="@+id/erase"
        android:background="@drawable/soundinv"
        android:focusable="false"
        android:focusableInTouchMode="false"
        /> 
   <TextView android:id="@+id/texxt1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/erase"
        android:layout_alignTop="@+id/erase"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#CC0"
   />

   <TextView android:id="@+id/texxt2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/texxt1"
        android:layout_alignLeft="@+id/texxt1"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#FFF"
   />
</RelativeLayout>

now you can also given click event to relative layout.

Defelice answered 31/5, 2011 at 7:32 Comment(1)
hey, check my xml again it's TwoLineListItem, and I found nothing wrong with itObolus
P
0

Just add this to your Java code: holder.yourButton.setFocusable(false); I am using my own cursorAdapter so I am putting the line of code at the end of bindView this should do it.

Pokey answered 21/5, 2013 at 6:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.