Android ListView with Button is not selectable(Clickable)
Asked Answered
B

2

8

I am facing a strange problem, I have added a Custom row in my ListView when I am removing the Button row is selectable, but as I add Button I am not able to click the row, please see the xml below.

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="77dp"
    android:layout_height="77dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="18dp"
    android:src="@drawable/company_logo" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Idds  sdsad "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/imageView1"

    android:layout_below="@+id/textView1"
    android:textColor="#8b8989"
    android:layout_marginLeft="5dp"
    android:text="Tap to see detail"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:text="Button" />

Please help why this is happening.

Brabazon answered 20/4, 2013 at 11:10 Comment(0)
C
20

Try setting

android:focusable="false"
android:focusableInTouchMode="false"

to your Button in the xml. The Button gain focus over the row, that's why you can't select your row.

Craw answered 20/4, 2013 at 11:12 Comment(0)
E
27

As you are using your custom row.

After setting onclickListener for button in your getView, set its focusability false.

i.e button.setFocusable(false);

And also set android:descendantFocusability="blocksDescendants" for your layout container of your row. You can directly set android:focusable="false" but this will make your button not clickable.

Expansive answered 20/4, 2013 at 11:14 Comment(2)
setting android:descendantFocusability="blocksDescendants" on the layout container of the row was sufficient to solve this problem for me. I didn't have to do anything with the button's focusable property.Marrakech
Even better solution, as it is more declarative and high-level. Very nice.Delate
C
20

Try setting

android:focusable="false"
android:focusableInTouchMode="false"

to your Button in the xml. The Button gain focus over the row, that's why you can't select your row.

Craw answered 20/4, 2013 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.